Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / Visual Basic

Getting All "Special Folders" via VB.NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
12 Apr 2016CPOL3 min read 16.4K   391   7   4
A VB.NET version of Ray Koopa's 21 Mar 2016 article, Getting All "Special Folders" in .NET

Introduction

On 21 Mar 2016, Ray Koopa published an article, Getting All "Special Folders" in .NET, which programmatically allowed C# users to extract the full path name of any of the Microsoft "Special Folders" built into versions of Windows® since Vista. His code works great! The only problem is that it is only written in C#, while many of us live in and program in a Visual Basic world. It is not our fault! It is a management directive, based on the idea that Visual Basic is easier for non-programmers to read and understand. (Sometimes, I think they would be happier with COBOL. On second thought, DO NOT TELL them about COBOL.NET or NetCobol!)

Background

As Ray Koopa pointed out in his article, beginning in Windows Vista, Microsoft added many more "special folders" to the ones such as My Documents and My Pictures. In the .NET libraries, Microsoft did not supply an easy-to-use solution to allow the programmer to retrieve these file paths. Ray goes into much more detail.

Ray Koopa's Solution

For C# programmers, Ray created a class he called KnownFolders with the following public methods, each of which has multiple signatures:

  • GetPath()
  • GetDefaultPath()
  • Initialize()

He also provided a short console routine to demonstrate the use of his code.

My Enhancements

I took his code and simply rewrote it as a Visual Basic module. Most of his original comments remain in the code. I did add an exception class, PathNotFoundException, derived from the ApplicationException class, to provide structured error handling.

Instead of a console routine, I included a Visual Basic Windows form to demonstrate the use of the code.

For the widest possible compatability, I built this with Visual Studio 2005. To the best of my knowledge, this module can be added to any Visual Basic in any version of Visual Studio after VS 2005.

Using the Code

To use the module, add KnownFolders.vb to your Visual Basic project.

To get the full path name to any of the "special folders," simply call Knownfolders.GetPath with the enumeration value for the folder path to be retreived. The call either returns the full path to the folder in question or throws the PathNotFoundException. The code below loops through the enumeration and adds a group of four lines to a text box:

  1. Numerical and string value of the "special folder" enumeration
  2. The current path or an error message
  3. The default path or an error message
  4. A blank line
VB.NET
For Each myFolder In [Enum].GetValues(GetType(KnownFolders.KnownFolder))
    Me.myText.Text &= CInt(myFolder).ToString & vbTab & myFolder.ToString & vbCrLf
    Me.myText.Text &= vbTab & "Current Path:" & vbTab
    Try
        Me.myText.Text &= KnownFolders.GetPath(myFolder)
    Catch ex As PathNotFoundException
        Me.myText.Text &= "**** " & ex.Message
    End Try
    Me.myText.Text &= vbCrLf
    Me.myText.Text &= vbTab & "Default Path:" & vbTab
    Try
        Me.myText.Text &= KnownFolders.GetDefaultPath(myFolder)
    Catch ex As Exception
        Me.myText.Text &= "**** " & ex.Message
    End Try
    Me.myText.Text &= vbCrLf & vbCrLf
Next

All you, the user has to do is append a "\" and then a filename. It is just that simple!

Points of Interest

I have noticed that many of the authors of Code Project libararies do not define their own ApplicationExceptions. They rely upon generating a MessageBox or triggering the UnhandledException handler from within their code. Defining your own exception is easy.

First, define an exception class:

VB.NET
Public Class PathNotFoundException
          Inherits ApplicationException
     Public Sub New(ByVal message As String
          MyBase.New(message)
     End Sub
End Class

Then, to use the exception, just throw it as shown:

VB.NET
Throw New PathNotFoundException( _
    "Unable to retrieve the known folder path.")

History

Dates are shown in ISO format.

  • 2016-04-07: My original release
  • 2016-03-21: Ray Koopa's release of Getting All "Special Folders" in .NET on CodeProject

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) S & D Acres
United States United States
BSEE 1972, Polytechnic Institute of Brooklyn, Brooklyn, NY
MSCS 1978, Stevens Institute, Hoboken, NJ

Now, I live in Texas with my wife and children and raise non-dairy (aka "meat") goats and chickens for fun and (maybe next year) profit.

As a hobby (we all need one, don't we?? Smile | :) ), I write agriculture-related software and play with O-gauge trains.

Comments and Discussions

 
QuestionMissing file(s) Pin
Philippe9113-Apr-16 2:48
Philippe9113-Apr-16 2:48 
AnswerRe: Missing file(s) Pin
Loves00723-Aug-16 5:39
Loves00723-Aug-16 5:39 
GeneralRe: Missing file(s) Pin
Philippe9123-Aug-16 6:06
Philippe9123-Aug-16 6:06 
GeneralRe: Missing file(s) Pin
Loves00724-Aug-16 10:46
Loves00724-Aug-16 10:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.