Check if full path given

hs2d picture hs2d · Apr 6, 2011 · Viewed 49.4k times · Source

Is there a method to check if given path is full path? Right now im doing this:

if (template.Contains(":\\")) //full path already given
{
}
else //calculate the path from local assembly
{
}

But there must be more elegant way for checking this?

Answer

detaylor picture detaylor · Apr 6, 2011

Try using System.IO.Path.IsPathRooted? It also returns true for absolute paths.

System.IO.Path.IsPathRooted(@"c:\foo"); // true
System.IO.Path.IsPathRooted(@"\foo"); // true
System.IO.Path.IsPathRooted("foo"); // false

System.IO.Path.IsPathRooted(@"c:1\foo"); // surprisingly also true
System.IO.Path.GetFullPath(@"c:1\foo");// returns "[current working directory]\1\foo"