10 Ways To Insure Project FailureIntroduction to VB.NET Generic Queues

Exploring the System.IO.Path Namespace - Part II

August 20th, 2007

exploring System.IO.PathIn this second part of my series on the System.IO.Path we will be taking a look at some additional functions in this namespace and see how they can be applied to your programs. You can read Part I of this series here: Exploring the System.IO.Path namespace - Part I.
.
.
.
.
GetRandomFileName

This interestingly named function returns a random string that you can use as either a folder name or a file name.

Dim NewFileName As String = IO.Path.GetRandomFileName()

NewFileName Example Value: "udhusxh3.0gh"

Unlike the Temp file functions, this function doesn’t create a file on the disk itself, it only provides you with a string of correctly formatted random characters. It is useful for creating your own random files but it is usually best from a security and rights standpoint to use the temp file creation functions instead.

GetTempFileName

As mentioned above, this function creates a new temp file on the disk in the appropriate temp file area for the user and returns the full file/path string to this newly created file.

Dim NewFileName As String = IO.Path.GetTempFileName()

NewFileName Example Value: "C:\Documents and Settings\username\Local Settings\Temp\tmp93D.tmp"
.
.
.

Using this method is preferred in situations where you may not have full disk access, such as in a ClickOnce deployment or deployment to secured desktops, because the temp area will be available and writable in most cases. It is important to remember to clean up after yourself if you this method. You don’t want to leave orphaned temp files behind.

GetTempPath

This function returns the name of the current user’s temp folder for the application as seen here.

Dim TempPath As String = IO.Path.GetTempPath()

TempPath Example Value: "C:\Documents and Settings\username\Local Settings\Temp\"
.
.
.

Usually the result will look like the path shown in the example above. However, you can’t depend on this and hard code this value. It is possible that the temp file location could change with a system configuration change. Also, if ClickOnce deployment is used, the temp file area may be in the ’sandbox’ and not the usual location. Using this function to get the location always returns reliable results for the current configuration.

HasExtension

This function checks a file/path string to see if it has an extension or not. Basically, it searches the specified string for a final period that doesn’t have any other file punctuation after it and has one or more other characters after it.

If IO.Path.HasExtension(NewFileName) Then

This function is moderately useful for checking if a string is only a path or a file name or path/file name combo. Note that it doesn’t indicate if the file exists on disk or not. It only tests the string.

IsPathRooted

This is another testing function that will tell you if the specified string contains a valid path or is just a file name. For example, if the string in question had only the file name but no path information, this function call would return False. However, it there was a valid path string before the file name, True would be returned.

If IO.Path.IsPathRooted(NewFileName) Then

Note that this function does not check for the exisistance of a path or file on the disk, it only checks the validity of the string itself. When combined with HasExtension it is quite useful in determining if a string can be safely used to construct a file.

File/Path Character Functions and Properties

Lastly, this namespace contains some functions and properties that can be helpful if you’re dealing with file systems outside of Windows, such as GetInvalidPathChars and GetInvalidFileNameChars. These probably won’t come into play much for you if you’re only working with Visual Studio in Windows. However, should you venture onto using .NET on other platforms, you might find them useful.

I hope these two articles were a good review of the System.IO.Path functions for you. Because the .NET Framework is so large it’s often easy to overlook areas of it. I know I found doing this review helpful to me and I hope you did too. 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 (1 votes, average: 4 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