Marine Corps Leadership Secrets - Part IVB.NET Link Round-Up for 6/26/07

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

July 26th, 2007

My.Computer.FileSystem ShortcutsThis article is the second in my series on the My.Computer.FileSystem Namespace. This namespace provides several functions for working with files. In this article we’ll take a look at some more functions and rate their usefulness based on how well they compare with the related NET Framework functions and if they bring any expanded or well wrapped functionality.


First let’s take a look at DirectoryExists:


This function simply returns if a target directory exists or not. It is exactly the same as this System.IO function:


Since this wrapper doesn’t bring anything new to the table, I’ll have to give it 2 out of 5.


Our next function is also an exact duplicate of the related System.IO function



Just like DirectoryExists, it doesn’t add anything new so I’ll rate it 2 out of 5 also.


Unlike our previous functions, our next one, FindInFiles, is quite useful. It searches a target folder and returns a read-only collection of strings that contains the matching files. Here’s an example of each overload:

Dim ListOfMatches As ObjectModel.ReadOnlyCollection(Of String) = _
                  My.Computer.FileSystem.FindInFiles(FolderToSearch, TextToSearch, _
                  True, FileIO.SearchOption.SearchTopLevelOnly)

Dim ListOfMatches As ObjectModel.ReadOnlyCollection(Of String) = _
                  My.Computer.FileSystem.FindInFiles(FolderToSearch, TextToSearch, _
                  True, FileIO.SearchOption.SearchTopLevelOnly, _
                  New String() {"s*.txt", "t*.txt"})


The search that this function wraps up is very powerful. Beyond just the directory, you also can specify the text to search for and to ignore the case of this text or not. Plus you can specify to search just the top level directory or the target directory and its sub-directories. The second overload also allows you to specify not only a single wildcard value but multiple wildcard values for a very powerful search.

Finally, the results come back in a type safe, read-only, string collection that’s very easy to work with. For example, if you wanted to load the list into a Listbox, you could do the following:

Dim FoundFiles(ListOfMatches.Count - 1) As String
ListOfMatches.CopyTo(FoundFiles, 0)

Since this function provides features above and beyond the related System.IO.Directory.GetFiles function with its ability to search the text of target files and wildcard support, I’ll have to give it a 5 out of 5 rating.


GetDirectories is similar to FindInFiles in that it searches a folder for matching directories, can recurse into subfolders, and can perform wildcard searches. Here is an example of the each overload:

ListOfMatches = My.Computer.FileSystem.GetDirectories(FolderToSearch)

ListOfMatches = My.Computer.FileSystem.GetDirectories(FolderToSearch, _
                FileIO.SearchOption.SearchAllSubDirectories, _
                New String() {"a*", "b*", "c*"})


The System.IO.Directory.GetDirectories is very similar to this function but there is one key difference. The System.IO function doesn’t allow multiple wildcards like this function, only a single string. This can be quite helpful if you’re needing to search a large directory structure for several different directories. Other than this, it is largely redundant so using this function vs. the System.IO function is dependant on your particular needs.

Because this function has an added value to it in one of its overloads, I’ll give it a 4 out of 5.

I’ll be covering more functions from My.Computer.FileSystem soon. 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 (3 votes, average: 3.33 out of 5)
Loading ... Loading ...

1 Comment Add your own

  • 1. irman  |  May 14th, 2009 at 12:38 am

    how to open specific file from VB.Net Windows application (Windows Form)

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