C#中带有非ASCII字符和FileInfo的文件路径 [英] File paths with non-ascii characters and FileInfo in C#

查看:378
本文介绍了C#中带有非ASCII字符和FileInfo的文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个或多或少看起来像这样的字符串:

I get a string that more or less looks like this:

"C:\\bláh\\bleh"

我用它制作一个FileInfo,但是当我检查它的存在时,它返回false:

I make a FileInfo with it, but when I check for its existence it returns false:

var file = new FileInfo(path);
file.Exists;

如果我手动将路径重命名为

If I manually rename the path to

"C:\\blah\\bleh"

在调试时

并确保其中存在污点,然后file.Exists开始返回true.因此,我认为问题在于非ASCII字符.

at debug time and ensure that blah exists with a bleh inside it, then file.Exists starts returning true. So I believe the problem is the non-ascii character.

实际的字符串是由我的程序构建的.一部分来自应用程序的AppDomain,该部分包含á",另一部分以某种方式来自用户.这两部分都由Path.Combine组合在一起.我通过两种方式确认了所得字符串的有效性:将程序生成的错误(包括路径)从错误中复制到资源管理器中,即可正常打开文件.在调试器中查看该字符串,它看起来正确地转义了,因为\被写为\. á"由调试器从字面上打印出来.

The actual string is built by my program. One part comes from the AppDomain of the application, which is the part that contains the "á", the other part comes, in a way, from the user. Both parts are put together by Path.Combine. I confirmed the validity of the resulting string in two ways: copying it from the error my program generates, which includes the path, into explorer opens the file just fine. Looking at that string at the debugger, it looks correctly escaped, in that \ are written as \. The "á" is printed literarily by the debugger.

我应该如何处理一个字符串,以便即使它包含非ASCII字符,它仍然是有效路径?

How should I process a string so that even if it has non-ascii characters it turns out to be a valid path?

推荐答案

问题是:程序没有足够的权限来访问该文件.修复权限可以解决此问题.看来,当我不进行实验时,我设法以某种方式设法重现了权限问题,可能是通过手工创建了不带非ascii字符的文件夹,然后复制了另一个文件夹.

The problem was: the program didn't have enough permissions to access that file. Fixing the permissions fixed the problem. It seems that when I didn't my experiment I somehow managed to reproduce the permission problem, possibly by creating the folder without the non-ascii character by hand and copying the other one.

哦...好尴尬.

这篇关于C#中带有非ASCII字符和FileInfo的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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