Tuesday, August 31, 2010

Some .Net 4 System.IO.Path static methods you might not know about…

Hajan's Blog - 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.

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

System.IO.Path.ChangeExtension() -…

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

- System.IO.Path.IsPathRooted() –

image…”

So much to learn… sigh. I love seeing little updates that will make my life easier. For example, seems like I play the ChangeExtension quite often. And now I can replace my custom code with this call. Man, I LOVE deleting code! lol

And Path.Combine? I use that ALL the time. This .Net 4 tweak to it will save me a number of lines of code. Man, I LOVE deleting code… :)

BTW, If you’re interested in seeing a little more about GetRandomFileName check out this recent DevCurry post, Random File Names using .NET

No comments: