2010-10-13 170 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