1

考虑以下代码片段

private void ProcessFile(string fullPath) {
    XmlTextReader rdr = new XmlTextReader("file:\\\\" + fullPath);
    while (rdr.Read()) {
        //Do something
    }
    return;
}

现在,当传递如下路径时,此功能正常:

“C:\工作文件\技术信息\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(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。
System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver ) 处的 Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn )
在 System.Threading.CompressedStack.runTryCode(Object userData)
在 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode 代码, CleanupCode backoutCode, Object userData)
在 System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state)
在 System.Xml.XmlTextReaderImpl.OpenUrl()
在 System.Xml.XmlTextReaderImpl.Read()
在 System.Xml.XmlTextReader.Read()

有人知道发生了什么吗?

4

3 回答 3

4

添加到 Konrad 的答案中,如果您使用的是 file:// 协议,则需要将 %23 用于 # 那么它可以正常工作

于 2008-10-21T09:08:07.653 回答
2

尝试省略file:///协议前缀。没有它对我有用。#我相信 .NET 会在它认为这是一个 URL之后截断任何部分。这只是基于错误消息的猜测,但考虑到#字符后面的部分不是由服务器处理而是由客户端在其他情况下(例如 Web 浏览器)处理,这似乎是合乎逻辑的。

于 2008-10-21T09:06:28.517 回答
0

你为什么不使用

XmlTextReader rdr = new XmlTextReader(fullPath);

于 2008-10-21T09:06:53.070 回答