在 WinApp 中,我只是想从 Uri 对象获取绝对路径:
Uri myUri = new Uri(myPath); //myPath is a string
//somewhere else in the code
string path = myUri.AbsolutePath;
如果我的原始路径中没有空格,这就可以正常工作。如果其中有空格,则字符串会被破坏;例如 'Documents and settings' 变成 'Documents%20and%20Setting' 等。
如有任何帮助,我们将不胜感激!
编辑: LocalPath 而不是 AbsolutePath 成功了!
最佳答案
这是应该的方式。这就是所谓的 URL 编码。它适用,因为 URL 中不允许有空格。
如果你想要包含空格的返回路径,你必须调用类似的东西:
string path = Server.URLDecode(myUri.AbsolutePath);
您无需导入任何内容即可在 Web 应用程序中使用它。如果出现错误,请尝试导入 System.Web.HttpServerUtility。或者,您可以这样调用它:
string path = HttpContext.Current.Server.URLDecode(myUri.AbsolutePath);
关于c# - Uri.AbsolutePath 用空格弄乱了路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/436520/