在C#中,如何检查路径是否为虚拟路径?

时间:2010-10-13 03:49:54

标签: c# asp.net file path

可能的虚拟路径:

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

6 个答案:

答案 0 :(得分:18)

这对我来说效果很好:

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

    return Server.MapPath(path);
}

答案 1 :(得分: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

答案 2 :(得分:1)

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

替代方案可能是System.IO.Path.GetPathRoot() - 如果它返回null则它是相对路径。然而,这有点像黑客,因为它对网络路径一无所知,所以如果它有效,它将巧合。

答案 3 :(得分:1)

  

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

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

我认为这是最容易出错的解决方案,因为您依靠Server.MapPath()的实现来确定失败的条件,而不是尝试创建尝试执行相同操作的冗余包装器。将来,MapPath()的功能可能会发生变化,实际上可能会开始支持虚拟路径,那么你可以使用代码来实际阻止这种使用。

答案 4 :(得分:1)

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

^(?:[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路径“\\”。

答案 5 :(得分:0)

// Path.IsPathRooted(&#34; /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

相关问题