How to tell if a path is relative or absolute

There is no function or method in the .NET framework to find this out directly.  I had though that Path.IsPathRooted(string) would do the job, but it seems that paths can be both relative, and rooted.  The path "C:bin\Debug\app.exe" is perfectly valid.  So, the only thing to do is to take the input, create the absolute path, and compare them.  Like this:

private void CheckDestination(string destination)
{
   // Ensure that path is absolute.  Ignore case
   // because filesystem is case-insensitive
   if (string.Compare(destination.Trim(),
      Path.GetFullPath(destination), true) != 0)
   {
        // path is absolute
   }
}

Leave a Reply