2010-10-13 168 views
15

可能的虛擬路徑:在C#中,你如何檢查路徑是否是虛擬的?

/folder1/folder2/image.jpg 
~/folder1/folder2/image.jpg 
folder1/folder2/image.jpg 

具體路徑:

C:\folder1\folder2\image.jpg 
D:\folder1\folder2\image.jpg 
C:/folder1/folder2/image.jpg 
C:/folder1\folder2/image.jpg 

你如何檢查路徑是否是虛擬的,或者是它的方式,這不是容易失敗?我問的原因是因爲當我在具體路徑上使用Server.MapPath()時,它會拋出異常。但是,我傳給Server.MapPath()的內容可以是我上面提供的任何一個示例,我不知道運行時間之前是什麼。

+1

你能不能檢查,如果它'X啓動:'? – BrunoLM 2010-10-13 03:59:17

回答

17

此作品不夠好,我說:

protected string GetPath(string path) 
{ 
    if (Path.IsPathRooted(path)) 
    { 
     return path; 
    } 

    return Server.MapPath(path); 
} 
+3

這不適用於'/ folder1/folder2/image.jpg',它將返回'true' – Jimbo 2013-11-28 11:06:36

+0

由於Path.IsPathRooted(「/ folder1/folder2/image.jpg」);'返回'true',它應該有而是返回'false'; – 2016-02-10 12:02:29

+0

這將在'某些'情況下工作,但會以「/」開頭的路徑失敗。 – user1751825 2016-04-26 06:09:35

1

我會使用反射器來檢查Server.MapPath()做什麼,並做到這一點。 :)

替代可能是System.IO.Path.GetPathRoot() - 如果它返回null那麼它是一個相對路徑。但是,這有點破解,因爲它不知道任何關於網絡路徑的信息,所以如果它有效的話,它會巧合運作。

8

Path.GetFullPath(string path)符合您的需求?您可以使用該方法,然後比較路徑是否改變。

if (path == Path.GetFullPath(path)) 
{ 
    // This is the full path (no changes) 
} 
else 
{ 
    // This is not the full path i.e. 'virtual' (changes) 
} 

參考: http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx

+0

我在這裏是新的,我如何取消我的操作? – 2016-04-26 08:54:17

1

之所以我問的是,因爲當我使用使用Server.Mappath()在具體路徑上,它會拋出異常

它是否爲此類型的條件拋出特定的異常,還是拋出通用的異常?如果異常特定於輸入路徑具體的條件,那麼我會在代碼中捕獲該特定異常。即使它是一個通用的異常,我仍然會捕獲這個異常,如果由於輸入路徑是虛擬的,您可能無法解碼,但您至少可以編寫自己的異常消息,包括通知它可以可能是由輸入路徑是虛擬的。

我相信這是最容易出錯的解決方案,因爲您依賴於Server.MapPath()的實現來確定失敗的條件,而不是試圖創建一個試圖做同樣事情的冗餘包裝器。將來MapPath()的功能可能會發生變化,它實際上可能會開始支持虛擬路徑,那麼實際上阻止這種使用的代碼會很愚蠢。

1

您可以使用以下正則表達式...

^(?:[a-zA-Z]:(?:\\|/)|\\\\) 

如果你想確保你總是有一個映射路徑。您可以使用以下一行...

VB。淨

my_path = If(Regex.IsMatch(my_path, "^(?:[a-zA-Z]:(?:\\|/)|\\\\)"), my_path, Server.MapPath(my_path)) 

C#

my_path = Regex.IsMatch(my_path, @"^(?:[a-zA-Z]:(?:\\|/)|\\\\)") ? my_path : Server.MapPath(my_path); 

這應該與普通驅動器路徑正常運行 「C:\」,以及UNC路徑 「\\」。

0

// Path.IsPathRooted(「/ folder1/folder2/image.jpg」);返回true

public void Main() 
     { 
      var path = @"/folder1/folder2/image.jpg"; 
      //is valid path? 
      if (!System.IO.Path.GetInvalidPathChars().Any(c => path.Contains(c.ToString()))) 
      { 

       if (IsAbsolutePhysicalPath(path)) 
       { 
        // This is the full path 
       } 
       else 
       { 
        // This is not the full path 
       } 

      } 
     } 
     private bool IsAbsolutePhysicalPath(string path) 
     { 
      if (path == null || path.Length < 3) 
       return false; 

      // e.g c:\foo 
      if (path[1] == ':' && IsDirectorySeparatorChar(path[2])) 
       return true; 

      // e.g \\server\share\foo or //server/share/foo 
      return IsUncSharePath(path); 
     } 
     private bool IsDirectorySeparatorChar(char ch) 
     { 
      return (ch == '\\' || ch == '/'); 
     } 

     private bool IsUncSharePath(string path) 
     { 
      // e.g \\server\share\foo or //server/share/foo 
      if (path.Length > 2 && IsDirectorySeparatorChar(path[0]) && IsDirectorySeparatorChar(path[1])) 
       return true; 
      return false; 

     } 

參考: http://referencesource.microsoft.com/#System.Web/Util/UrlPath.cs,5e5cf20a50a858e2

+0

使用正則表達式可以更輕鬆地完成此操作。正則表達式可以在1行中完成同樣的事情。 – user1751825 2016-04-28 04:00:46