在C#中,你如何检查是否路径是虚拟的或没有? [英] In C#, how do you check if a path is virtual or not?

查看:307
本文介绍了在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()可以是我上面提供的例子中的任何一个,我不知道它是什么之前,运行时间。

How do you check whether a path is virtual or not in a way that's not prone to failure? The reason why I'm asking is because when I use Server.MapPath() on a concrete path, it will throw an exception. However, what I'm passing to Server.MapPath() can be any one of the examples I provided above and I don't know what it is before run-time.

推荐答案

此作品不够好,我:

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

    return Server.MapPath(path);
}

这篇关于在C#中,你如何检查是否路径是虚拟的或没有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆