Manipulating Filepaths In .NET

December 12, 2007

If I had a Dollar for every time I wrote a few lines of code to change a file extension on a path (e.g. MyPart.slddrw to MyPart.pdf) or to build a filepath from a folderpath and a filename then I would probably have a few thousand dollars.  In other words, enough to buy a loaf of bread in any developed country outside the US.  (Note to self:  Stay on topic and don’t get distracted by a rant on the pathetic state of the dollar)

 Anyways, my colleague Evan Basalik showed me a very easy way to do this with minimal code.  Even though I thought I had been all over the System.IO namespace, I never checked out the System.IO.Path section.  Here are some of the cool functions you will find in there:

 

 system.io.path.combine   - This function combines a folderpath and a filename to form a filepath.  The best part is you don’t have to check the folderpath for a “\” which makes it probably my favorite function in the namespace. 

The purpose of the remaining functions are a mystery : )  based on their obscure names.:

system.io.path.ChangeExtension 

system.io.path.GetFilename

system.io.path.GetDirectoryName

system.io.path.GetExtension

system.io.path.GetFileNameWithoutExtension

I encourage you guys to check out these functions.  I can’t believe I never saw them before.  If anyone has any indispensible little functions that are built-in to .NET then feel free to share them in the comments.

If you enjoyed this post, make sure you subscribe to my RSS feed!

Comments

One Response to “Manipulating Filepaths In .NET”

  1. Josh on December 14th, 2007 8:56 am

    Jeff, you are so manipulative.

Got something to say?