Working with System.IO.Path static class

Hello everyone.

Today, I was exploring the System.IO namespace in .NET 4.0 on an ASP.NET web application where I came to few changes, comparing with the previous versions of .NET, related to the System.IO.Path static class. The Path class contains the same method names in .NET 2.0, 3.5 as well as 4.0, only (in .NET 4.0) new overloads are added to System.IO.Path.Combine() method.

In the following blog post, I will show the usage of few methods belonging to the System.IO.Path static class, as well as, the changes in Combine() method added in .NET 4.0.

Besides the first method that is available in the previous versions for combining two string paths, now in .NET 4.0 you can combine unlimited strings to the path.

Note: For testing purpose, on your web application root, create folders with name Files, Files/SubFolder, Files/SubSubFolder and add three text files: text1.txt (in Files), text2.txt (in Files/SubFolder), text3.txt (in Files/SubFolder/SubSubFolder).

 

Here are few examples:

            string path1 = System.IO.Path.Combine(Server.MapPath("Files"), "text1.txt"); //this will give the path to %WEBAPP_ROOT%/Files/text1.text -
            string path2 = System.IO.Path.Combine(Server.MapPath("Files"), "SubFolder", "text2.txt"); //this will give the path to %WEBAPP_ROOT%/Files/SubFolder/text2.text -
            string path3 = System.IO.Path.Combine(Server.MapPath("Files"), "SubFolder", "SubSubFolder", "text3.txt"); //this will give the path to %WEBAPP_ROOT%/Files/SubFolder/SubSubFolder/text3.text -

            string[] pathArr = { Server.MapPath("Files"), "SubFolder", "SubSubFolder", "text3.txt" }; //you can add unlimited array of strings and Combine them using System.IO.Path.Combine(pathArr)

            //example
            string path4 = System.IO.Path.Combine(pathArr);

            //print result
            Response.Write("With two string paths:"+path1 + "<br /> With three string paths:" + path2 + "<br /> With four string paths:" + path3 + "<br /> Using string array:"+path4);

In previous versions of .NET Framework, we have option to Combine only two paths. Now, new flexibility to this method is added to unlimited paths.

However, the System.IO.Path static class contains some other very useful methods.

Lets pass through few of them (that I like the most).

- System.IO.Path.ChangeExtension() - method has ability to change the extension of the file included as the first parameter in this method. This is very good way if you want to manipulate files, where you won’t need to use RegEx, splitting or some other methods to change the file extension. You should remember that the change of the extension is made only virtually, where the file in the file system remains with the old extension, .txt in our case.

System.IO.Path.ChangeExtension(System.IO.Path.Combine(Server.MapPath("Files"), "text1.txt"), ".jpg")


- System.IO.Path.GetExtension()
– method that has simple functionality. To extract the file extension of a given file path.

System.IO.Path.GetExtension(System.IO.Path.Combine(Server.MapPath("Files"),"text1.txt"))


- System.IO.Path.GetRandomFileName() & System.IO.Path.GetTempFileName() - are both used to create random file name. The difference is that using GetRandomFileName() method, the file is created virtually and only the fileName is returned as string, whereas the GetTempFileName() method created zero-byte temporary file on disk and returns full path to the filename i.e. C:\Users\Hajan\AppData\Local\Temp\tmpA50E.tmp.

System.IO.Path.GetRandomFileName()
System.IO.Path.GetTempFileName()

Both methods have no parameters!


- System.IO.Path.IsPathRooted() – method that returns Boolean true/false checking whether the path is absolute or relative.

            System.IO.Path.IsPathRooted("Files/text1.txt"); //will return False
            System.IO.Path.IsPathRooted(System.IO.Path.Combine(Server.MapPath("Files"), "text1.txt")); //will return True

 

There are other methods such as:

            System.IO.Path.GetDirectoryName("<path>"); //returns the directory name from given path
            System.IO.Path.GetFileName("<path>"); //returns the file name with extension from the specified <path>
            System.IO.Path.GetFileNameWithoutExtension("<path>") //returns the file name without extension from the specified <path>

and few other methods for similar purpose as the previous explained ones.

 

I hope this was an useful and informative blog post.

Thank you for reading,
Hajan

2 Comments

Comments have been disabled for this content.