简体   繁体   中英

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. 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."

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)

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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