1. Technology

Special Directories and System Directories in VB.NET

Using the My namespace, and finding the path for the rest of the folders too!

By

Reached my first Forms Application of Visual Basic.NET, the first app of my career Jan 2010 - full screen
gopal1035/Flikr/CC BY 2.0
Updated June 18, 2014

Since VB.NET 2005, Visual Basic programmers have enjoyed a feature not available in other .NET languages: the My namespace. The My namespace doesn't actually provide completely new capability to your program. You could get the same information in previous versions and in other languages (and if you reference the VB.NET library in your C# program, you can use it there too with just a little extra effort). But the My namespace gives you access to properties and methods in seven groups of software objects more quickly and easily than was possible before. These groups of objects are:

  • My.Application
  • My.Computer
  • My.Forms
  • My.Resources
  • My.Settings
  • My.User
  • My.WebServices

The illustration below shows how VB.NET's Intellisense can point you in the right direction once you have a basic understanding of what's available in the My objects.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Windows puts specific types of files in specific locations these days. For example, there is support 'built in' to Vista to import and manage images in MyPictures. So applications often need to know the actual path to these directories. The problem is that the location might change. XP and Vista, for example, don't use the same path for everything. In Vista, Programs is in a "roaming" directory:

C:\Users\<computername>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs

So ...

My.Computer.FileSystem.SpecialDirectories

... can be very handy to span the differences between operating systems and custom modifications that might have been made to individual computers. The SpecialDirectories available are:

  • AllUsersApplicationData
  • CurrentUserApplicationData
  • Desktop
  • MyDocuments
  • MyMusic
  • MyPictures
  • ProgramFiles
  • Programs
  • Temp

You can save a temporary file in using the path returned:

Dim DirPath As String
DirPath = _
   My.Computer.FileSystem.SpecialDirectories.Temp
MsgBox(DirPath)
My.Computer.FileSystem.CopyFile( _
   "C:\AFile.txt", _
   DirPath & _
   "\Whatever.ext")

(This example copies a file from the root to the Temp directory, but you could use WriteAllBytes or a variety of other code depending on the program requirements once you have the path to the Temp directory.)

But we know that there are more 'special' directories than this! What about your Internet cookies for example? How about the startup directory? There's a way to get to those too! Use the Environment class in System. This class has a lot more in it (like TickCount, the number of milliseconds since the system started). In addition, you can use the SpecialFolder Enum to get the same information in SpecialDirectories plus more.

To see the directories that you can access this way, use this code to list the SpecialFolder Enum:

Dim SpFolders As Type = _
   GetType(Environment.SpecialFolder)
Dim Sp As String
For Each Sp In [Enum].GetNames(SpFolders)
   Console.WriteLine(Sp & " -- " & [Enum].Parse(SpFolders, Sp))
Next

Note: In case you're not familiar with the syntax, Enum is both a keyword and a class in VB.NET. You have to tell VB.NET that you don't want it to be used as a keyword by placing it in brackets. You could also fully qualify it. So either of these syntax forms would work.

System.Enum.Parse

[Enum].Parse

The code snippet shown gives you a list like this:

Desktop -- 0
Programs -- 2
Personal -- 5
MyDocuments -- 5
Favorites -- 6
Startup -- 7
Recent -- 8
SendTo -- 9
StartMenu -- 11
MyMusic -- 13
DesktopDirectory -- 16
MyComputer -- 17
Templates -- 21
ApplicationData -- 26
LocalApplicationData -- 28
InternetCache -- 32
Cookies -- 33
History -- 34
CommonApplicationData -- 35
System -- 37
ProgramFiles -- 38
MyPictures -- 39
CommonProgramFiles -- 43

You can retrieve the path that you need to use these additional directories using the GetFolderPath method. Here's the code to do that:

Dim DirPath As String
DirPath = _
   System.Environment.GetFolderPath( _
   Environment.SpecialFolder.Favorites)

And you should be set to go!

  1. About.com
  2. Technology
  3. Visual Basic
  4. Using VB.NET
  5. Programming Special Folders in VB.NET

©2014 About.com. All rights reserved.