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:
- Numerical and
string
value of the "special folder" enumeration
- The current path or an error message
- The default path or an error message
- A blank line
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:
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:
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