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
Path.Combine(System.Environment.ExpandEnvironmentVariables("%HOMEDRIVE%"),
Path.Combine(System.Environment.ExpandEnvironmentVariables("%HOMEPATH%"), "Desktop"));
Example 2:
Consider you want to read a text file residing in the user local profile.
using (StreamReader sr =
new StreamReader(Path.Combine(System.Environment.ExpandEnvironmentVariables("%LOCALAPPDATA%"),
"myFile.txt")))
{
// Read file
}
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
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
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
System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop
Example 2:
Consider you want to read a text file residing in the user local profile.
using (StreamReader sr = new StreamReader(Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), ?myFile.txt?)))
{
// Read file
}
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:
- MSDN: Using Environemnt variables in cmd.exe
- MSDN: System.IO.Path
- MSDN: System.Environment
- Clounce.com: Combining Directory Paths
Excerpt from MSDN: Using Environemnt variables in cmd.exe article
Variable | Type | Description |
---|---|---|
%ALLUSERSPROFILE% | Local | Returns the location of the All Users Profile. |
%APPDATA% | Local | Returns the location where applications store data by default. |
%CD% | Local | Returns the current directory string. |
%CMDCMDLINE% | Local | Returns the exact command line used to start the current Cmd.exe. |
%CMDEXTVERSION% | System | Returns the version number of the current Command Processor Extensions. |
%COMPUTERNAME% | System | Returns the name of the computer. |
%COMSPEC% | System | Returns the exact path to the command shell executable. |
%DATE% | System | Returns 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% | System | Returns the error code of the most recently used command. A non zero value usually indicates an error. |
%HOMEDRIVE% | System | Returns 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% | System | Returns 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% | System | Returns 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% | Local | Returns the name of the domain controller that validated the current logon session. |
%NUMBER_OF_PROCESSORS% | System | Specifies the number of processors installed on the computer. |
%OS% | System | Returns the operating system name. Windows 2000 displays the operating system as Windows_NT. |
%PATH% | System | Specifies the search path for executable files. |
%PATHEXT% | System | Returns a list of the file extensions that the operating system considers to be executable. |
%PROCESSOR_ARCHITECTURE% | System | Returns the chip architecture of the processor. Values: x86, IA64. |
%PROCESSOR_IDENTIFIER% | System | Returns a description of the processor. |
%PROCESSOR_LEVEL% | System | Returns the model number of the processor installed on the computer. |
%PROCESSOR_REVISION% | System | Returns the revision number of the processor. |
%PROMPT% | Local | Returns the command prompt settings for the current interpreter. Generated by Cmd.exe. |
%RANDOM% | System | Returns a random decimal number between 0 and 32767. Generated by Cmd.exe. |
%SYSTEMDRIVE% | System | Returns the drive containing the Windows XP root directory (that is, the system root). |
%SYSTEMROOT% | System | Returns the location of the Windows XP root directory. |
%TEMP% and %TMP% | System and User | Returns 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% | System | Returns 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% | LOCAL | Returns the name of the domain that contains the user’s account. |
%USERNAME% | Local | Returns the name of the user who is currently logged on. |
%USERPROFILE% | Local | Returns the location of the profile for the current user. |
%WINDIR% | System | Returns the location of the operating system directory. |