How To Uninstall a Failed Windows Service InstallationSite News for 8/13/07 through 8/18/07

Exploring the System.IO.Path Namespace - Part I

August 17th, 2007

exploring System.IO.PathIn my series on the My.Computer.FileSystem namespace I had overlooked some of the powerful file name parsing capabilities made available in the System.IO.Path namespace. The .NET Framework is quite vast and it’s easy to stick with what we encounter and use first rather than exploring other areas. Plus, it’s easy to forget parts of the Framework that you may not use everyday. Therefore, I thought it would be a good idea to explore the functions in this namespace for both my own knowledge and yours.

ChangeExtension

This function replaces the current extension on a path/file string with the one specified. For example, if you had a raw data file and your program generated a matching report that needed to have the same file name, this function makes this process easy as seen here:

ReturnValue = IO.Path.ChangeExtension(RawDataFile, "rpt")

This function doesn’t create a new file or directory, it only serves as a path/file string parser. Also, it only affects the last extension so if you had a path/file with the value “C:\reports\stores.07.08.17.dat” the resulting value would be “C:\reports\stores.07.08.17.rpt”. Also use care to avoid placing invalid file/path characters into either parameter in order to avoid generating an exception.

Combine

This function is similar to the CombinePath function found in the My.Computer.FileSystem that I mentioned in my previous article. It takes two path parts and combines them into a single, well formatted, path string as shown here:

ReturnValue = IO.Path.Combine("c:\docs\customers\", "smith.doc")

ReturnValue is: c:\docs\customers\smith.doc

As I mentioned before, this function seems rather redundant with String.Concat although it adds the ability deal with periods or extra or lacking backslashes. It also has the same quirk as seen here:

ReturnValue = IO.Path.Combine("c:\docs\customers\", "\smith\smith2.doc")

This returns

\smith\smith2.doc

and not

c:\docs\customers\smith\smith2.doc

Like you might expect.

This function produces slightly different results than the My.Computer.FileSystem under some conditions. I’m not sure why this happens but you should be aware of this. Like the My function, I don’t think this function is as reliable as a plain old String.Concat is so I don’t see it as something I would use in one of my programs.

GetDirectoryName

As you would expect, GetDirectoryName returns the path portion of a file/path string. For example, this call

ReturnValue = IO.Path.GetDirectoryName("c:\docs\customers\smith.doc")

would return

c:\docs\customers

One thing to remember is that it does not return a trailing backslash so you’ll need to remember to include it in any string concatinations you do with the result.

GetExtension

This function is yet another file/path string parsing function and it returns just the extension as seen here:

ReturnValue = IO.Path.GetDirectoryName("c:\docs\customers\smith.doc")

ReturnValue is: ".doc"

ReturnValue = IO.Path.GetDirectoryName("c:\docs\customers\smith")

Return Value is: ""

ReturnValue = IO.Path.GetDirectoryName("c:\docs\customers\")

ReturnValue is: ""

If there is no extension or if there is no file name, a blank string is returned. Also, notice that the returned value includes the leading period, something to take into account for concatinations.

GetFileName

Another file/path parsing function, this one returns both the base file name and extension with no path information:

ReturnValue = IO.Path.GetFileName("c:\docs\customers\smith.doc")

ReturnValue is: "smith.doc"

ReturnValue = IO.Path.GetFileName("c:\docs\customers")

ReturnValue is: "customers"

ReturnValue = IO.Path.GetFileName("c:\docs\customers\")

ReturnValue is: ""

Be careful with this function though because it will return the final part of a path as a file name if there is no trailing backslash to indicate a path.

GetFileNameWithoutExtension

Yes, it’s another obvious file/path parser, this time returning the file name with no extension.

ReturnValue = IO.Path.GetFileName("c:\docs\customers\smith.doc")

ReturnValue is: "smith"

ReturnValue = IO.Path.GetFileName("c:\docs\customers")

ReturnValue is: "customers"

ReturnValue = IO.Path.GetFileName("c:\docs\customers\")

ReturnValue is: ""

As with GetFileName, it will assume the last part of the path is a file name if there is not a trailing backslash.

GetFullPath

This function returns the qualified file/path string. This function can be handy if you need to convert a relative path to an absolute, qualified path, as seen here:

ReturnValue = IO.Path.GetFileName("..\docs\customers\smith.doc")

ReturnValue is: "C:\Program Files\My Report Program\docs\customers\smith.doc"

Note that the appended physical path will be based on the current application’s location.

GetPathRoot

This function returns the root directory of a file/path string as shown here:

ReturnValue = IO.Path.GetPathRoot("C:\docs\customers\smith.doc")

ReturnValue is: "C:\"

ReturnValue = IO.Path.GetPathRoot("\\myserver\archive\smith\smith2.doc")

ReturnValue is: "\\myserver\archive"

As you can see, the backslash isn’t included in the parsing of a URI path string while it is for a local or mapped drive.

I hope this was a useful review of the System.IO.Path functions for you. I’ll post Part II in a few days where we’ll cover some of the other functions in this namespace. If you have a question or thoughts on this namespace, please feel free to leave a comment.

Share This Article: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • Technorati
  • DotNetKicks
  • DZone

Entry Filed under: VB.NET Tutorials


Rate This Article:

Not That GoodCould Be BetterOKGoodGreat (3 votes, average: 3.67 out of 5)
Loading ... Loading ...

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Most Popular Articles

Highest Rated Articles

Categories

Most Recent Articles

Feeds

 Subscribe in a reader

To subscribe by e-mail
Enter your address here

Delivered by FeedBurner

VB Opportunities

Archives