C#File.ReadAllText返回"NotSupportedException" [英] C# File.ReadAllText returning "NotSupportedException"

查看:103
本文介绍了C#File.ReadAllText返回"NotSupportedException"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

File.ReadAllText似乎有问题,因为即使目标文件存在,它也会返回"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();
        }
    }
}

是的,此目录中确实存在Text.txt. StreamReader具有完全相同的问题.有什么解决方法吗?

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

推荐答案

如果您解码字符串

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

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

如您所见,路径以奇怪的 U202a 字符开始,该字符是双向文本控制字符

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

,因此不能用作作为路径名的一部分,因此得到NotSupportedException(文件系统不支持路径名中的U202a)

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)

这篇关于C#File.ReadAllText返回"NotSupportedException"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆