Check whether the path is absolute or Relative?

To check whether a given path is absolute or relative, we have a method called IsPathRooted in 'Path' Class. If the given path is absoulte path it will return true otherwise false.

According to MSDN , System.IO.Path.IsPathRooted() method Gets a value indicating whether the specified path string contains a root.The IsPathRooted method returns true if the first character is a directory separator character such as "\", or if the path starts with a drive letter and colon (:). For example, it returns true for path strings such as "\\MyDir\\MyFile.txt", "C:\\MyDir", or "C:MyDir". It returns false for path strings such as "MyDir".

Note: This method does not verify that the path or file name exists.


Ex:
string sPath="C:\Windows";
if(Path.IsPathRooted(sPath))
 lblMessage.Text="Path s absolute";
else
 lblMessage.Text="Path s relative";


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

System.IO.Path class provides many useful methods to work with the file paths. You can check them in the msdn here.
http://msdn.microsoft.com/en-us/library/3bdzys9w

Gopikrishna

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment