2009-01-12 56 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)