Windows 中的长路径错误?
我有以下文件:
C:\Users\Jan\Documents\Visual Studio 2010\Projects\AzureTests\Build\82df3c44-0482-47a7-a5d8-9b39a79cf359.cskpg\WebRole1_778722b2-eb95-476d-af6a-917f269a0814.cssx\39e5cb39-cd18-4e1a-9c25-72bd1ad41b49.csman
我可以通过记事本++中的打开窗口或通过资源管理器打开此文件。但是,通过“运行”窗口打开不起作用。它给出一个“找不到文件”对话框。当我在 C# 中使用以下命令查询文件系统时:
var dir = new DirectoryInfo(@"C:\Users\Jan\...")
var fil = dir.GetFiles("*.csman")[0];
该文件也在返回的文件列表中,但我无法执行以下操作:
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(fil.FullName);
因为此操作会失败,并出现“(1,1) 处的数据不正确”错误。因为 XmlDocument
认为该文件是空的。但是,对此文件的 File.ReadAllBytes
会成功。这有效:
var buf = File.ReadAllBytes(fil.FullName);
using (var ms = new MemoryStream())
{
ms.Write(buf, 0, (int) buf.Length);
ms.Seek(0, SeekOrigin.Begin);
xmlDoc.Load(ms);
}
调用时不会出现问题...
xmlDoc.Save(fil.FullName);
有人可以解释这里发生了什么吗?
I have the following file:
C:\Users\Jan\Documents\Visual Studio 2010\Projects\AzureTests\Build\82df3c44-0482-47a7-a5d8-9b39a79cf359.cskpg\WebRole1_778722b2-eb95-476d-af6a-917f269a0814.cssx\39e5cb39-cd18-4e1a-9c25-72bd1ad41b49.csman
I can open this file fine via the open window in notepad++, or via the explorer. However, opening via the Run window doesn't work. It gives an 'cannot find the file' dialog. When I query the filesystem in C# with:
var dir = new DirectoryInfo(@"C:\Users\Jan\...")
var fil = dir.GetFiles("*.csman")[0];
The file is also in the list of returned files but I can't do a:
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(fil.FullName);
Because this fails with an 'incorrect data at (1,1)' error. Because the XmlDocument
thinks the file is empty. However a File.ReadAllBytes
on this file succeeds. This works:
var buf = File.ReadAllBytes(fil.FullName);
using (var ms = new MemoryStream())
{
ms.Write(buf, 0, (int) buf.Length);
ms.Seek(0, SeekOrigin.Begin);
xmlDoc.Load(ms);
}
The problem doesn't occur when calling...
xmlDoc.Save(fil.FullName);
Can someone explain what is happening here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XmlDocument.LoadXml
需要一个直接包含 XML 数据的字符串。因此,它将路径字符串解释为 XML(这显然是无效的,这就是抛出异常的原因)。
请改用
XmlDocument.Load
方法。调用
XmlDocument.Save
,因为与Load
一样,它的单个参数代表文件的路径。基本上,您遇到的有点长的文件路径是一个转移注意力的东西,而不是您面临的问题的根本原因。
XmlDocument.LoadXml
expects a string that directly contains the XML data.It is therefore interpreting the path-string as if it were XML (which will obviously be invalid, which is why the exception is thrown).
Use the
XmlDocument.Load
method instead.You don't face the problem when calling
XmlDocument.Save
, because, likeLoad
, it's single parameter represents the path to the file.Basically, the somewhat long file-path you've got there is a red-herring and not the root-cause of the issue you are facing.
还有你的另一个问题:
如果路径名中有空格,Windows“运行”需要用引号将路径名括起来。
And your other problem:
Windows "Run" requires quotes around the path name if there are spaces in it.