C# File.ReadAllText 返回 "NotSupportedException"
C# File.ReadAllText returning "NotSupportedException"
File.ReadAllText 似乎有问题,因为即使目标文件存在,它也会返回 "NotSupportedException"。无论在参数中放入什么,它都会抛出相同的异常。
using System;
using System.IO;
namespace MyNameSpace
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(File.ReadAllText(@"C:\Test.txt"));
Console.ReadKey();
}
}
}
是的... Text.txt 确实存在于此目录中。 StreamReader 有完全相同的问题。有什么解决方法吗?
编译器注释:"Additional information: The given path's format is not supported."
根据MSDN,一个NotSupportedException
表示:
path is in an invalid format.
可能您的路径中有一个不可见的字符,或者您的实际代码中缺少逐字运算符 (@
),使 \t
成为制表符。
如果你解码字符串
String report = String.Join(" ", @"C:\Test.txt".Select(c => ((int) c).ToString("x4")));
Console.Write(report);
你会得到
202a 0043 003a 005c 0054 0065 0073 0074 002e 0074 0078 0074
如您所见,路径以奇怪的 U202a 字符开始,它是 双向文本控制字符
https://en.wikipedia.org/wiki/Unicode_control_characters
因此 不能用作 作为路径名的一部分所以你得到 NotSupportedException
(文件系统不支持 U202a
在路径名中)
在我的例子中,我遇到了同样的异常,但这是管理员权限未设置到特定驱动器。我以管理员身份打开 VSTS,然后 运行 相同的程序正常运行。
File.ReadAllText 似乎有问题,因为即使目标文件存在,它也会返回 "NotSupportedException"。无论在参数中放入什么,它都会抛出相同的异常。
using System;
using System.IO;
namespace MyNameSpace
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(File.ReadAllText(@"C:\Test.txt"));
Console.ReadKey();
}
}
}
是的... Text.txt 确实存在于此目录中。 StreamReader 有完全相同的问题。有什么解决方法吗?
编译器注释:"Additional information: The given path's format is not supported."
根据MSDN,一个NotSupportedException
表示:
path is in an invalid format.
可能您的路径中有一个不可见的字符,或者您的实际代码中缺少逐字运算符 (@
),使 \t
成为制表符。
如果你解码字符串
String report = String.Join(" ", @"C:\Test.txt".Select(c => ((int) c).ToString("x4")));
Console.Write(report);
你会得到
202a 0043 003a 005c 0054 0065 0073 0074 002e 0074 0078 0074
如您所见,路径以奇怪的 U202a 字符开始,它是 双向文本控制字符
https://en.wikipedia.org/wiki/Unicode_control_characters
因此 不能用作 作为路径名的一部分所以你得到 NotSupportedException
(文件系统不支持 U202a
在路径名中)
在我的例子中,我遇到了同样的异常,但这是管理员权限未设置到特定驱动器。我以管理员身份打开 VSTS,然后 运行 相同的程序正常运行。