C# File.ReadAllText returning “NotSupportedException”

扶醉桌前 提交于 2019-12-11 00:27:11

问题


There seems to be a problem with File.ReadAllText because it's returning "NotSupportedException" even when the target file exists. 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. StreamReader has the exact same problem. Is there any workaround for this?

Compiler notes: "Additional information: The given path's format is not supported."


回答1:


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

As you can see, the path starts with the strange U202a character which is Bidirectional text control character

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)




回答2:


According to MSDN, a NotSupportedException indicates:

path is in an invalid format.

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.




回答3:


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.



来源:https://stackoverflow.com/questions/37654824/c-sharp-file-readalltext-returning-notsupportedexception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!