Accessing the correct Windows special folder

When developing Windows Applications it is quite common to end up having to access a special folder. Some examples of special folders are user Desktop, user Documents (My Documents in case of Windows XP), temporary folder, etc. When using the file system I/O functions it is important to always access the correct folder thus one cannot assume that for example the user Desktop is always located at “C:\Users\<user name>\Desktop” or “C:\Documents and Settings\<user name>\Desktop”.

Part 1: The most generic way of accessing special folders

Windows provides environmental variables to access special folders, for example %HOMEPATH% to access the user folders and %PROGRAMFILES% to access the Program Files folder. These environmental variables can be used in .NET to obtain the correct folder location for special folders.

The complete list of common environmental variables can be obtained from “Using Environemnt variables in cmd.exe” article on MSDN. For quick referencing we have taken the relevant excerpt from MSDN and appended it at the end of this article.

In order to use the environmental variables from .NET code one has to use the method

System.Environment.ExpandEnvironmentVariables(string)

The static method ExpandEnvironmentVariables() converts all environment variables within the specified path to their correct value.

Example 1:

For example, consider you need to access the user Desktop, the C♯ code to do so is

  1. Path.Combine(System.Environment.ExpandEnvironmentVariables("%HOMEDRIVE%"), 
  2. Path.Combine(System.Environment.ExpandEnvironmentVariables("%HOMEPATH%"), "Desktop"));

Example 2:

Consider you want to read a text file residing in the user local profile.

  1. using (StreamReader sr = 
  2.     new StreamReader(Path.Combine(System.Environment.ExpandEnvironmentVariables("%LOCALAPPDATA%"), 
  3.                      "myFile.txt")))
  4. {
  5. // Read file
  6. }

The drawback of this approach is that some environment variables might not exist on all platforms or the way the paths are returned change slightly from one platform to another.

Part 2: Using .NET enumerations

Another way of accessing special folders in .NET is to use

  1. System.Environment.SpecialFolder

enumerations. These enumerations provide a reliable way of accessing special folders on different platforms. Values from this enumeration can be used with the method

  1. System.Environment.GetFolderPath()

Taking the same examples used in Part 1, the code using the GetFolderPath() method will look like

Example 1:

For example, consider you need to access the user Desktop, the C♯ code to do so is

  1. System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop

Example 2:

Consider you want to read a text file residing in the user local profile.

  1. using (StreamReader sr = new StreamReader(Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), ?myFile.txt?)))
  2. {
  3. // Read file
  4. }

When comparing the code in Path 1 and the code above it is clear that there is no structural change. However, using the GetFolderPath() method provides a more reliable and access to more special folders as it removes the operating system dependency from code.

Part 3: The exception

The temporary folder is the mostly used folder. .NET provides another alternative way of accessing the temporary folder. System.IO.Path.GetTempPath() method returns the system?s temporary folder. Using this method guarantee?s that when writing or reading files stored in the temporary folders you always access the current temporary folder.

References:

Excerpt from MSDN: Using Environemnt variables in cmd.exe article

VariableTypeDescription
%ALLUSERSPROFILE%LocalReturns the location of the All Users Profile.
%APPDATA%LocalReturns the location where applications store data by default.
%CD%LocalReturns the current directory string.
%CMDCMDLINE%LocalReturns the exact command line used to start the current Cmd.exe.
%CMDEXTVERSION%SystemReturns the version number of the current Command Processor Extensions.
%COMPUTERNAME%SystemReturns the name of the computer.
%COMSPEC%SystemReturns the exact path to the command shell executable.
%DATE%SystemReturns the current date. Uses the same format as the date /t command. Generated by Cmd.exe. For more information about the date command, see Date
%ERRORLEVEL%SystemReturns the error code of the most recently used command. A non zero value usually indicates an error.
%HOMEDRIVE%SystemReturns which local workstation drive letter is connected to the user’s home directory. Set based on the value of the home directory. The user’s home directory is specified in Local Users and Groups.
%HOMEPATH%SystemReturns the full path of the user’s home directory. Set based on the value of the home directory. The user’s home directory is specified in Local Users and Groups.
%HOMESHARE%SystemReturns the network path to the user’s shared home directory. Set based on the value of the home directory. The user’s home directory is specified in Local Users and Groups.
%LOGONSERVER%LocalReturns the name of the domain controller that validated the current logon session.
%NUMBER_OF_PROCESSORS%SystemSpecifies the number of processors installed on the computer.
%OS%SystemReturns the operating system name. Windows 2000 displays the operating system as Windows_NT.
%PATH%SystemSpecifies the search path for executable files.
%PATHEXT%SystemReturns a list of the file extensions that the operating system considers to be executable.
%PROCESSOR_ARCHITECTURE%SystemReturns the chip architecture of the processor. Values: x86, IA64.
%PROCESSOR_IDENTIFIER%SystemReturns a description of the processor.
%PROCESSOR_LEVEL%SystemReturns the model number of the processor installed on the computer.
%PROCESSOR_REVISION%SystemReturns the revision number of the processor.
%PROMPT%LocalReturns the command prompt settings for the current interpreter. Generated by Cmd.exe.
%RANDOM%SystemReturns a random decimal number between 0 and 32767. Generated by Cmd.exe.
%SYSTEMDRIVE%SystemReturns the drive containing the Windows XP root directory (that is, the system root).
%SYSTEMROOT%SystemReturns the location of the Windows XP root directory.
%TEMP% and %TMP%System and UserReturns the default temporary directories that are used by applications available to users who are currently logged on. Some applications require TEMP and others require TMP.
%TIME%SystemReturns the current time. Uses the same format as the time /t command. Generated by Cmd.exe. For more information about the time command, see Time
%USERDOMAIN%LOCALReturns the name of the domain that contains the user’s account.
%USERNAME%LocalReturns the name of the user who is currently logged on.
%USERPROFILE%LocalReturns the location of the profile for the current user.
%WINDIR%SystemReturns the location of the operating system directory.