1

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
}
brad
  • 73,826
  • 21
  • 73
  • 85
  • In what scope are you talking about "virtual"? There are virtual folders in IIS, and there is the concept of a virtual path in ASP.NET. The two are not necessarily the same. – jrista Aug 20 '09 at 20:53
  • I mean a virtual path in asp.net. Thanks for the catch. – brad Aug 20 '09 at 20:55

2 Answers2

3

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
  • 19,343
  • 2
  • 29
  • 25
  • 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 Aug 20 '09 at 20:56
  • 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 Aug 20 '09 at 21:04
3

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

Eirik H
  • 654
  • 2
  • 8
  • 30
JBrooks
  • 9,901
  • 2
  • 28
  • 32