【问题标题】:In C#, how do you check if a path is virtual or not?在 C# 中,如何检查路径是否为虚拟路径?
【发布时间】:2011-04-24 15:41:22
【问题描述】:

可能的虚拟路径:

/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() 的内容可以是我上面提供的任何一个示例,我不知道它在运行时之前是什么。

【问题讨论】:

  • 你不能检查它是否以X:开头吗?

标签: c# asp.net file path


【解决方案1】:

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

【讨论】:

  • 我是新来的,如何取消我的操作?
【解决方案2】:

您可以使用以下正则表达式...

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

如果您想确保始终拥有映射路径。您可以使用以下单线...

VB.Net

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 路径“\\”。

【讨论】:

    【解决方案3】:

    // Path.IsPathRooted("/folder1/folder2/image.jpg");返回真

     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

    【讨论】:

    • 这可以用正则表达式更容易地完成。正则表达式可以在 1 行中执行完全相同的操作。
    【解决方案4】:

    我问的原因是因为当我在具体路径上使用 Server.MapPath() 时,它会抛出异常

    对于这种类型的条件,它会抛出一个特定的异常,还是会抛出一个通用的异常?如果异常特定于输入路径具体的条件,我会在您的代码中捕获该特定异常。即使它是一个通用异常,我仍然会捕获这个异常,如果它被抛出,你可能无法解码,因为输入路径是虚拟的,但你至少可以编写自己的异常消息,包括它的通知 可能是由于输入路径是虚拟的。

    我相信这是最不容易出错的解决方案,因为您依赖 Server.MapPath() 的实现来确定它失败的条件,而不是尝试创建一个尝试做同样事情的冗余包装器。将来 MapPath() 的功能可能会发生变化,它实际上可能会开始支持虚拟路径,那么如果你有代码来实际阻止这种使用,那就太愚蠢了。

    【讨论】:

      【解决方案5】:

      这对我来说效果很好:

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

      【讨论】:

      • 这不适用于/folder1/folder2/image.jpg,它将返回true
      • 投反对票,因为Path.IsPathRooted("/folder1/folder2/image.jpg"); 返回true 而它应该返回false
      • 这在“某些”情况下有效,但对于以“/”开头的路径会失败。
      • 在检查前添加path.TrimStart('\\', '/') 怎么样?
      【解决方案6】:

      我会使用 Reflector 来检查 Server.MapPath() 做了什么并做到了。 :)

      另一种可能是System.IO.Path.GetPathRoot() - 如果它返回null,那么它是一个相对路径。不过,这有点小题大做,因为它对 Web 路径一无所知,所以如果它有效,那将是巧合。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-20
        • 2022-01-19
        • 2013-03-30
        • 1970-01-01
        • 1970-01-01
        • 2014-05-05
        • 2011-12-26
        相关资源
        最近更新 更多