views:

94

answers:

2

Is there a built-in asp.net method for checking the "virtualness" of a path?

The only way I've been able to do it so far is with the following try block:

public void Foo(String path){

    try
    {
        path = Server.MapPath(path);
    }
    catch(HttpException){}

    // do stuff with path
}
+2  A: 

Would the Path.IsPathRooted method work?

You're resulting code would be:

public void Foo(String path)
{
    if(!Path.IsPathRooted(path))
    {
        path = Server.MapPath(path);
    }

    // do stuff with path
}
akmad
Woohoo! That works like a charm. I wish the terminology was a bit more consistent though. Is a non-rooted path the same as a virtual path?
brad
Like you said, this is mostly a problem of terminology. You are using "virtual" but a more correct term would be "relative". Any path that doesn't have an absolute path (ie C:\Folder\file.txt) would therefore have to be relative to the current directory.
akmad
+2  A: 

Here is everything you need to know about ASP.Net paths: Rick Strahl's post "Making Sense of ASP.Net Pahts"

JBrooks
+1 for Strahl - a genius who I rarely understand. Great link by the way, really helped me out.
adrianos