Uri.AbsolutePath 用空格弄乱了路径
在 WinApp 中,我只是尝试从 Uri 对象获取绝对路径:
Uri myUri = new Uri(myPath); //myPath is a string
//somewhere else in the code
string path = myUri.AbsolutePath;
如果我的原始路径中没有空格,则效果很好。 如果其中有空格,则字符串会被破坏; 例如“文档和设置”变为“文档%20和%20设置”等。
任何帮助将不胜感激!
编辑: LocalPath 而不是 AbsolutePath 就成功了!
In a WinApp I am simply trying to get the absolute path from a Uri object:
Uri myUri = new Uri(myPath); //myPath is a string
//somewhere else in the code
string path = myUri.AbsolutePath;
This works fine if no spaces in my original path. If spaces are in there the string gets mangled; for example 'Documents and settings' becomes 'Documents%20and%20Setting' etc.
Any help would be appreciated!
EDIT:
LocalPath instead of AbsolutePath did the trick!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这就是它应该的样子。 这就是所谓的 URL 编码。 它适用是因为 URL 中不允许有空格。
如果您希望返回包含空格的路径,则必须调用类似以下内容的内容:
您不应该被要求导入任何内容才能在 Web 应用程序中使用它。 如果出现错误,请尝试导入 System.Web.HttpServerUtility。 或者,您可以这样称呼它:
This is the way it's supposed to be. That's called URL encoding. It applies because spaces are not allowed in URLs.
If you want the path back with spaces included, you must call something like:
You shouldn't be required to import anything to use this in a web application. If you get an error, try importing System.Web.HttpServerUtility. Or, you can call it like so:
它按应有的方式对其进行编码,您可能可以对其进行 UrlDecode 以使用空格将其恢复,但它并没有“损坏”,只是正确编码了。
我不确定你在写什么,但要将其转换回 asp.net,它是 Server.UrlDecode(path)。 如果它是 Windows 应用程序,您也可以使用 LocalPath,而不是 AbsolutePath。
It's encoding it as it should, you could probably UrlDecode it to get it back with spaces, but it's not "mangled" it's just correctly encoded.
I'm not sure what you're writing, but to convert it back in asp.net it's Server.UrlDecode(path). You also might be able to use LocalPath, rather than AbsolutePath, if it's a Windows app.
只需使用 uri.LocalPath 即可
Just use uri.LocalPath instead
Uri 还有一些静态方法 - EscapeDataString 和转义UriString。
Uri.EscapeDataString(uri.AbsolutePath)
也适用Uri also has a couple of static methods - EscapeDataString and EscapeUriString.
Uri.EscapeDataString(uri.AbsolutePath)
also works使用 HttpUtility:
Use HttpUtility: