Home About Us SQL Interview Book Contact Us RSS
Code Snippets
Tips & Tricks
Code Snippets

Saravana Kumar
Vinod Kumar

System.IO.Path Class in .NET

In this code snippet I am going to focus on Path class in .NET. This is one of .NET framework class which is not known to many users in .NET, so I am going to explain some of the useful methods provided by this class.  Path class performs operations on String  instances that contain file or directory path information.   Parsing or manipulating a file path previous to .NET release involves quite a lot of work. But in .NET, the path class will greatly reduce the development time by providing various methods to operate on path string.

The members of the Path class enable you to quickly perform some common operations such as determining whether a file name extension is part of a path,  combining two strings into one path string, finding filename or extension from a path. You are going to see some of the common operations you will be doing using Path class in this article. To start with, most frequent operation which you want to do on path string is to get the filename or file extension from a path. In Path class you have a static method called getFileName or getExtension to get filename or file extension from a path class.

        Dim strPath As String = "d:\temp\ExtremeExperts.txt" 

        Dim strURL As String = "http:\\www.extremeexperts.com\general\home\homepage.aspx" 

        Dim temp As String

        temp = Path.GetFileName(strPath)     'ExtremeExperts.txt

        temp = Path.GetExtension(strPath)    '.txt

        temp = Path.GetFileName(strURL)      'homepage.aspx

Next if you want to get the filename without extension, then you can use getfilenamewithoutextension method to get the filename alone. For example,

         temp = Path.GetFileNameWithoutExtension(strPath)      'ExtremeExperts

         temp = Path.GetFileNameWithoutExtension(strURL)        'homepage

If you want to get the root directory from a path, then we can use GetPathRoot method to get the root directory name,

         temp = Path.GetPathRoot(strPath)                              'd:\

Another most frequent operation is appending filename with any path string, while doing this you should take care whether  "/" is already there at the end of the path string  otherwise you need to append "/" along with filename. But using combine method in path class, you no need to worry about all these things. Just pass two strings to combine method, one is path and another is filename. It will return the combined string.

          temp = Path.Combine("d:\temp\", "ExtremeExperts.txt")     'd:\temp\ExtremeExperts.txt

          temp = Path.Combine("d:\temp", "ExtremeExperts.txt")      'd:\temp\ExtremeExperts.txt

If you want to create a temporary file in your machine, then using path class you just need one line to create that file. For example,

        Dim tempfilename As String

        tempfilename = Path.GetTempFileName

        Dim sw As StreamWriter = New StreamWriter(tempfilename)

        ' Add some text to the file.

        sw.WriteLine("Creating a temp file.")


        sw.Write("The date is: ")



 GetTempFileName method of path class will create an empty file with unique name and return the filename as string. Then using streamWriter method you can write anything to that file. Next if you want to change the file extension, there is another method called Changeextension in Path class which will does the work for you,

       temp = Path.ChangeExtension(strPath, ".aspx")        ' d:\temp\ExtremeExperts.aspx 

Like this you can do many more operation with Path class which will  simplify your work. All these operations are performed on strings, these methods wont verify whether returned strings are valid . For example, in the previous example you have changed the file extension of a file. That just change the file extension in that string, it wont create new file with that extension in that path.

Since path might vary with respect to various platforms, Path class also has few static properties with which you  can set properties specific to platforms. For example, DirectorySepartorChar, PathSepartor, VolumeSepartorChar are few properties which can be used to set platform specific properties before you start work on this Path class.To know more about Path, check out this MSDN documention about Path class

Here are the article's link which explains about few other useful .NET Framework classes.