2009-01-12 110 views
9

在WinApp我只是想从一个Uri对象获得的绝对路径路径:Uri.AbsolutePath弄乱了空间

Uri myUri = new Uri(myPath); //myPath is a string 
//somewhere else in the code 
string path = myUri.AbsolutePath; 

这工作正常,如果我原来的路径没有空格。如果有空格,那么这个字符串就会被破坏;例如'Documents and settings'成为'Documents%20and%20Setting'等。

任何帮助将不胜感激!

编辑: 本地路径,而不是AbsolutePath的伎俩!

+0

纸条给所有:这是编码无知 – JohnIdol 2009-01-12 21:23:50

回答

9

它编码它,因为它应该,你可能可能UrlDecode它取回空格,但它不是“损坏”它只是正确的编码。

我不确定你在写什么,但要在asp.net中将其转换回Server.UrlDecode(path)。如果它是Windows应用程序,您也可以使用LocalPath,而不是AbsolutePath。

+0

谢谢您的回答罕见的显示 - 该怎么办我这样做? – JohnIdol 2009-01-12 18:52:13

+0

我不确定你在写什么,但在asp.net中它是Server.UrlDecode(path); – 2009-01-12 18:53:26

+1

如果是Windows应用程序,您也许可以使用LocalPath而不是AbsolutePath。 – 2009-01-12 18:55:02

12

这是应该的方式。这就是所谓的网址编码。它适用,因为空间不允许在URL中。

如果你想要的路径回来包含空格,则必须调用是这样的:

string path = Server.URLDecode(myUri.AbsolutePath); 

你不应该需要导入任何在web应用中使用此。如果您遇到错误,请尝试导入System.Web.HttpServerUtility。或者,你可以调用它像这样:

string path = HttpContext.Current.Server.URLDecode(myUri.AbsolutePath); 
1

乌里也有一对夫妇的静态方法 - EscapeDataString和EscapeUriString。

Uri.EscapeDataString(uri.AbsolutePath)也适用

3

只需使用uri.LocalPath代替

0

使用HttpUtility:

HttpUtility.UrlDecode(uri.AbsolutePath)