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
   }
}
This entry was posted in C# and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">