C#: Find location of My Documents, Application Data, Program Files and other Windows directories

May 6, 2008 by Dave
 

I’ve seen the (perhaps VB-style) way of getting the MyDocuments folder, by using %HOMEDIR%, a batch-style variable.  Some of these environment variables can be modified by navigating to Control Panel -> System -> Advanced (tab).  In Vista, you’ll need to select Advanced System Settings at the System page in order to invoke the System Properties dialog.

These environment varibles work as pathes, but they are not really the accepted way of doing things.  What you should do, is use System.Environment.GetFolderPath().  This method takes a value from the System.Environment.SpecialFolder enum.  GetFolderPath will return a string for your location.  This is supported on all versionf of the .NET framework.

Below is an excerpt from MSDN of the possible values for SpecialFolder:

Name Description
ApplicationData The directory that serves as a common repository for application-specific data for the current roaming user.
CommonApplicationData The directory that serves as a common repository for application-specific data that is used by all users.
LocalApplicationData The directory that serves as a common repository for application-specific data that is used by the current, non-roaming user.
Cookies The directory that serves as a common repository for Internet cookies.
Desktop The logical Desktop rather than the physical file system location.
Favorites The directory that serves as a common repository for the user’s favorite items.
History The directory that serves as a common repository for Internet history items.
InternetCache The directory that serves as a common repository for temporary Internet files.
Programs The directory that contains the user’s program groups.
MyComputer The “My Computer” folder.
MyMusic The “My Music” folder.
MyPictures The “My Pictures” folder.
Recent The directory that contains the user’s most recently used documents.
SendTo The directory that contains the Send To menu items.
StartMenu The directory that contains the Start menu items.
Startup The directory that corresponds to the user’s Startup program group.
System The System directory.
Templates The directory that serves as a common repository for document templates.
DesktopDirectory The directory used to physically store file objects on the desktop.
Personal The directory that serves as a common repository for documents.
MyDocuments The “My Documents” folder.
ProgramFiles The program files directory.
CommonProgramFiles The directory for components that are shared across applications.

Comments