c# - Uri.AbsolutePath 用空格弄乱了路径

标签 c# .net-2.0 system.net winapp

在 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/

相关文章:

c# - 检查 HttpStatusCode 是否表示成功或失败

c# - httpWebRequest.GetResponse() 方法中的 Operation Timed out exception 是否关闭连接

c# - 使用 Razor 内联设置 javascript 变量,这是好的做法/安全吗?

c# - 并行处理并将列表一分为二 - 它会加速整个操作吗?

c# - 登录后 Azure AD SSO 重定向

.net - 当 .NET 崩溃报告中的 P9 "bucket"包含乱码而不是导致崩溃的异常名称时,这意味着什么?

c# - xsd.exe 生成类的可空值

c# - 将文件保存在临时目录中是否被认为是好的/可接受的做法?

c# - 使用新成员反序列化对象

c# - 在 httplisteners authenticationselectordelegate 中提供身份验证被拒绝的消息