Wednesday, March 14, 2007
Sometimes I see a method and say "Where have you been all my life?"  When it's in code I wrote over a year ago, I have to say "How did I ever let you go?"  I'm writing this so I don't ever forget it again and to introduce the most useful class that holds it.

The method in question is quite simple, but very useful...

Path.GetFileName(SomeFilePath)

It takes a full file path and just returns the File Name and Extension. Very handy when uploading files or checking URLs.  So useful, I wrote my own version of the function and put it in the base web page class of my projects.   So today I am wandering through some code and there it was.  I used it in a method that I wrote almost 2 years ago and then FORGOT about it. Duh, talk about reinventing the wheel. 

And there are more wheels....

Path.GetFileNameWithoutExtension(SomeFilePath)

Nice! Saves an IndexOf or Split call to get the goodies.

Path.ChangeExtension(SomeFilePath, NewExtension)

Quick change file extension and it makes sure there is a dot between them.

Path.GetPathRoot(SomeFilePath)

Gets the other side of the file path.

But the real nice one is this one...

Path.Combine(SomeFilePath1, SomeFilePath2)

This is the one I REALLY wish I knew about.  There are so many things that need to be checked to join two parts of a path together properly.  Is one of the parts blank?  Does part 1 end with a path separator, does part 2 start with one?  This one call replaces 12-15 lines of code. 

Oy, if I had only remembered this class, it could have saved me a few hours of my life. Need to investigate the Framework a little more thoroughly in the future. 

As for the Path class -- she's never leaving my side again. 

Wednesday, March 14, 2007 9:11:17 PM (Eastern Standard Time, UTC-05:00)   #     Comments [0]  | 

Theme design by Dean Fiala

Pick a theme: