简体   繁体   English

C#File.ReadAllText返回“NotSupportedException”

[英]C# File.ReadAllText returning “NotSupportedException”

There seems to be a problem with File.ReadAllText because it's returning "NotSupportedException" even when the target file exists. File.ReadAllText似乎存在问题,因为即使目标文件存在,它也会返回“NotSupportedException”。 No matter what is put into the parameter, it keeps throwing the same exception. 无论放入什么参数,它都会不断抛出相同的异常。

using System;
using System.IO;

namespace MyNameSpace
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(File.ReadAllText(@"‪C:\Test.txt"));
            Console.ReadKey();
        }
    }
}

And yes... Text.txt does exist in this directory. 是的... Text.txt确实存在于此目录中。 StreamReader has the exact same problem. StreamReader有完全相同的问题。 Is there any workaround for this? 这有什么解决方法吗?

Compiler notes: "Additional information: The given path's format is not supported." 编译器注释:“附加信息:不支持给定路径的格式。”

If you decode the string 如果你解码字符串

String report = String.Join(" ", @"‪C:\Test.txt".Select(c => ((int) c).ToString("x4")));

Console.Write(report);

You'll get 你会得到

202a 0043 003a 005c 0054 0065 0073 0074 002e 0074 0078 0074 202a 0043 003a 005c 0054 0065 0073 0074 002e 0074 0078 0074

As you can see, the path starts with the strange U202a character which is Bidirectional text control character 如您所见,路径以奇怪的U202a字符开头,该字符是双向文本控制字符

https://en.wikipedia.org/wiki/Unicode_control_characters https://en.wikipedia.org/wiki/Unicode_control_characters

and thus can't be used as a part of a path name and so you get NotSupportedException (File System doesn't support U202a in the path name) 因此不能用作路径名的一部分,因此您得到NotSupportedException (文件系统在路径名中不支持U202a

According to MSDN , a NotSupportedException indicates: 根据MSDNNotSupportedException指示:

path is in an invalid format. path的格式无效。

Probably there is a non-visible character in your path, or your verbatim operator ( @ ) is missing in your actual code, making \\t a tab character. 可能在您的路径中有一个不可见的字符,或者您的实际代码中缺少您的逐字运算符( @ ),使得\\t成为制表符。

In My case I faced the same Exception but it was Administrator permission not set to the particular drive. 在我的情况下,我遇到了相同的异常,但管理员权限未设置为特定驱动器。 I opened VSTS as an administrator and ran the same program then it worked properly. 我以管理员身份打开VSTS并运行相同的程序然后它正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM