Extensible CAD Technologies
SolidWorks and PDMWorks Enterprise Development Blog

Manipulating Filepaths In .NET

Posted by Jeff Cope at 12 December, 2007, 2:32 am

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!

Category : .NET

One Response to “Manipulating Filepaths In .NET”


Josh December 14, 2007

Jeff, you are so manipulative.



Generate First-Article Inspection Reports From TIF, PDF and CAD Drawing Files With InspectionXpert. Generate First-Article Inspection Reports From SolidWorks Drawing Files With InspectionXpert.