How To Make Debugging .NET Windows Services EasierMarine Corps Leadership Secrets - Part I

My.Computer.FileSystem Shortcuts for VB.NET - Part I

July 25th, 2007

My.Computer.FileSystem ShortcutsThe My.Computer.FileSystem Namespace provides several ’speed dials’ for working with files. In this article we’ll take a look at some of them and see where they’re useful, where they’re redundant with existing .NET Framework functions and where they’re troublesome. Also, I’ll provide my rating for each function.


First, let’s take a look at CombinePath. This function takes two path parts and combines them into a single, well formatted, path string. Here’s an example using a path and file name :

NewPath = My.Computer.FileSystem.CombinePath("c:\docs\customers\", "smith.doc")

This looks rather redundant with String.Concat at first glance. However, it does add a little extra functionality in some cases. For example, if a path contains heading periods or extra or lacking backslashes the function will take care of this. But it also has it’s quirks, as seen here:

NewPath = My.Computer.FileSystem.CombinePath("c:\docs\customers\", "\smith\smith2.doc")


This returns


and not


Like you might expect. However, if you were to have a leading period or periods on the second string the result would be the second one. Things get even more quirky when you add network URI’s into the mix.

I give this function 2 out of 5 due to its quirky nature and that using a regular String.Concat will produce more predictable results with less chance for an exception.


The second function we’ll take a look at is CopyDirectory. This function copies a directory and its contents from a source location to a target location and has overloads to overwrite and to provide a UI. Here’s an example of each usage:


My.Computer.FileSystem.CopyDirectory(sourcePath, targetPath, True)

My.Computer.FileSystem.CopyDirectory(sourcePath, targetPath, _
                                                     Microsoft.VisualBasic.FileIO.UIOption.AllDialogs) _
                                                     My.Computer.FileSystem.CopyDirectory(sourcePath, targetPath, _
                                                     Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, _


This function is quite handy since it combines several copying functions from System.IO into a single function. The option to overwrite or not overwrite is also quite useful. My only complaint about this function is the UI options since they don’t provide an easy way to insert your own dialog(s) into the mix.

I give this function a 4 out of 5 since it is a work saver although it could be better.


Now let’s take a look at the CopyFile function. This function simply copies a file from one place to another and has options to overwrite and to show UI just like the CopyDirectory function. Here are the examples:

My.Computer.FileSystem.CopyFile(sourceFile, targetFile, )

My.Computer.FileSystem.CopyFile(sourceFile, targetFile, True)

My.Computer.FileSystem.CopyFile(sourceFile, targetFile, _

My.Computer.FileSystem.CopyFile(sourceFile, targetFile, _
                                              Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, _


This function, unlike CopyDirectory, is rather redundant with the System.IO.File.Copy function. This limits it’s usefulness to times when you want to have the UI visible. Otherwise, it’s usually better just to use the System.IO.File method. One thing to remember about either method is that the file permissions are copied over with the file so make sure you take this into account.

I give this function 3 out of 5 since it’s largely redundant with a .NET Framework function.


Next on our list for today is CreateDirectory. Obviously this function creates a directory, like so:


Like CopyFile, this function is also redundant with a System.IO function but, even worse, it’s not as functional as the Framework version. System.IO.Directory.CreateDirectory also has an overload that allows you to set access control for the newly created folder.

In this case, this function isn’t much of a speed dial so I’ll give it 2 out of 5.


The last function for this part is DeleteDirectory, which, of course, is used to delete a directory. Unlike the copy functions above it does offer features above and beyond its System.IO counterpart, System.IO.Directory.Delete. Here are what it’s various overloads look like:

My.Computer.FileSystem.DeleteDirectory("c:\docs\jones", _

My.Computer.FileSystem.DeleteDirectory("c:\docs\jones", _
                                                       Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs, _

My.Computer.FileSystem.DeleteDirectory("c:\docs\jones", _
                                                       Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, _
                                                       FileIO.RecycleOption.DeletePermanently, _


As you can see, you can’t simply enter the folder name, you have to specify if you want to delete all contents or throw an exception if the directory isn’t empty. In the other overloads you can also chose to show the UI and to decide if the deleted contents should be deleted permenantly or sent to the recycle bin. It would have been nice if they had combined these options a little better where you could have had the UI and recycle options along with the content option.

On this one, I’ll give it 3.5 out of 5 because the extensions to the base System.IO functionality, while decent and relatively useful, aren’t that big of a functionality boost.

I’ll be publishing more on this series in the next few days. Please leave me a comment if your rating of these functions is different from mine.

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

Entry Filed under: VB.NET Tutorials

Rate This Article:

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

3 Comments Add your own

  • 1. Evan  |  March 19th, 2008 at 8:20 am

    The copyfile command is great for local stuff, but I have searching hi and low for how to copy a file over a network. For example…
    I need to copy a selected file “strFileToCopy” to another server in an unshared folder “strDestination” which would be \\server01\winnt\system32″. How would i go about coding this baby?
    Any help would be greatly appreciated.

  • 2. jeremy  |  September 12th, 2008 at 11:57 am

    it’s not quirky at all if you look at what it is doing.

    if you tell it c:\directory\someotherDirectory\ and then tell it, “from there, go to \thisplace.ext,” it’s doing exactly what you said. “Go down two levels, then go to \ (root) and find the path to the file thisplace.ext.”

    If you put a dot in front of the \ then you’re indicating “this directory”. you’re saying “start at \directory\someotherDirectory, and then go to the directory you’re in (.) and find the path for the file thisplace.ext.”

    If you passed it \\thisserver\shareA\ and also \\otherserver\shareB\file.ext it should return \\otherserver\shareB\file.ext — you just said “go to thisserver, find shareA, and from there go to \\ (new server) otherserver and find shareB and return the path to the file there named file.ext.

  • 3. Filiberto  |  December 18th, 2008 at 11:00 am

    Hi, which reference should I activate so that I can activate My.Computer.Filesystem shortcuts?

Leave a Comment


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

Visit Me At My New Site, Programming In C#

Most Popular Articles

Highest Rated Articles


Most Recent Articles


 Subscribe in a reader

To subscribe by e-mail
Enter your address here

Delivered by FeedBurner

VB Opportunities