2012-03-01 117 views

回答

13

Uri班是你的朋友。

提供統一資源標識符(URI)的對象表示形式,並可輕鬆訪問URI的各個部分。

IsFile將嘗試確定Uri確實指向一個文件。

使用Segements屬性,以獲取文件名(這將是最後一段)。

Uri uri = new Uri("http://example.com/title/index.htm"); 
var filename = uri.Segments[uri.Segments.Length - 1]; 
// filename == "index.htm" 
+0

感謝/效果很好,除了ISFILE給了我假的這個uriString中:[鏈接](HTTP ://www.example.com/foo/bar/banner/enu_wide_small_219x113_white.gif) – Muleskinner 2012-03-01 15:59:22

+0

isFile將只返回true,如果url的格式爲file:// – Lukos 2018-01-02 15:17:48

0

您可以使用Server.MapPath()從虛擬路徑映射物理路徑。

此外,還有許多的HTTPUtility中的方法,可以幫助你繪製各種不同類型的路徑。

0

方法有很多種,主要被描述here
Baqsically如果需要利用Uri類,也許串tokeniztion。

1

您可能會粘在創建開放的對象,如果你關心與此類似性能使用的東西:

public class UriHelpers 
    { 
     public static string GetFileNameFromUrl(string url) 
     { 
      string lastSegment = url.Split('/').Last(); 

      return lastSegment.Substring(0, lastSegment.IndexOf('?') < 0 ? lastSegment.Length : lastSegment.IndexOf('?')); 
     } 
    }