Path tweaking

There are a couple of useful methods in the Path class of System.IO namespace. If you ever wanted to find out the full path to a file, get the file name without an extension or combine two paths, this class gives you all the weapons needed for it. So, here is a compilation of the methods found in the Path class. Note: This is not complete. I haven’t covered all the methods.For the complete list, visit the msdn page.

Have: C:\mydocs\file.txt
Want: C:\mydocs\file.doc
Method to use: ChangeExtension

Have: C:\mydocs and personal\file.doc
Want: C:\mydocs\personal\file.doc
Method to useCombine

Have: C:\mydocs\personal\file.doc
Want: C:\mydocs\personal
Method to use: GetDirectoryName

Have: C:\mydocs\personal\file.doc
Want: .doc
Method to use: GetExtension

Have: C:\mydocs\personal\file.doc
Want: file.doc
Method to use: GetFileName

Have: C:\mydocs\personal\file.doc
Want: file
Method to use: GetFileNameWithoutExtension

Have: file.doc
Want: C:\mydocs\personal\file.doc
Method to use: GetFullPath

Have: C:\mydocs\personal\file.doc
Want: C:\
Method to use: GetPathRoot

Note: The output of certain Uses depend on the current working directory. Ensure that the Use returns what you expect.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: