路径中的哈希字符引发 DirectoryNotFoundException
考虑以下代码片段
private void ProcessFile(string fullPath) {
XmlTextReader rdr = new XmlTextReader("file:\\\\" + fullPath);
while (rdr.Read()) {
//Do something
}
return;
}
现在,当传递如下路径时,此功能正常:
“C:\Work Files\Technical Information\Dummy.xml”,
时会抛出错误
但在传递“C:\Work Files\#Technical Information\Dummy.xml” "
请注意,指定的所有文件夹和文件都存在,并且哈希字符是路径的有效字符。 错误详细信息是:
System.IO.DirectoryNotFoundException:找不到路径“C:\Work Files\”的一部分。
在 System.IO.__Error.WinIOError(Int32 errorCode, String MaybeFullPath)
在System.IO.FileStream.Init(字符串路径,FileMode模式,FileAccess访问,Int32权限,布尔useRights,FileShare共享,Int32 bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,字符串msgPath,布尔bFromProxy)
at System.IO.FileStream..ctor(字符串路径、FileMode 模式、FileAccess 访问、FileShare 共享、Int32 bufferSize)
位于System.Xml.XmlDownloadManager.GetStream(Uri uri,ICredentials凭据)
位于System.Xml.XmlUrlResolver.GetEntity(Uri绝对Uri,字符串角色,ObjectToReturn类型)
位于System.Xml.XmlTextReaderImpl.OpenUrlDelegate(对象xmlResolver)
位于System.Threading.CompressedStack.runTryCode(Object userData)
位于System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode代码,CleanupCode backoutCode,对象userData)
位于System.Threading.CompressedStack.Run(CompressedStackcompressedStack,ContextCallback回调,对象状态)
在 System.Xml.XmlTextReaderImpl.OpenUrl()
在 System.Xml.XmlTextReaderImpl.Read()
在 System.Xml.XmlTextReader.Read()
有人知道发生了什么事吗?
Consider the following code snippet
private void ProcessFile(string fullPath) {
XmlTextReader rdr = new XmlTextReader("file:\\\\" + fullPath);
while (rdr.Read()) {
//Do something
}
return;
}
Now, this functions fine when passed a path like:
"C:\Work Files\Technical Information\Dummy.xml"
But throws an error when passed
"C:\Work Files\#Technical Information\Dummy.xml"
Note that all folders and files specified exist and that the hash character is a valid character for paths. The error details are:
System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Work Files\'.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver)
at System.Threading.CompressedStack.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state)
at System.Xml.XmlTextReaderImpl.OpenUrl()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlTextReader.Read()
Anybody know what's going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
添加到 Konrad 的答案中,如果您使用 file:// 协议,则需要使用 %23 for # 那么它就可以正常工作
Adding to Konrad's answer, if you are using the file:// protocol, you need to use %23 for # then it works fine
尝试省略
file:///
协议前缀。 它对我来说没有任何作用。 我相信如果 .NET 认为这是一个 URL,它会截断#
之后的任何部分。 这只是基于错误消息的猜测,但考虑到#
字符后面的部分不是由服务器处理,而是由其他场景(例如 Web 浏览器)中的客户端处理,这似乎是合乎逻辑的。Try omitting the
file:///
protocol prefix. It works for me without one. I believe .NET will truncate any part after the#
if it believes this to be a URL. This is only a guess based on the error message but it seems logical considering that the part after the#
character isn't processed by the server but rather by the client in other scenarios (e.g. web browsers).为什么不使用
XmlTextReader rdr = new XmlTextReader(fullPath);
Why don't you use
XmlTextReader rdr = new XmlTextReader(fullPath);