.NET 4.0+Identifying .NET 4.0 Special Folders
The Microsoft Windows environment includes many special folders, the paths of which can be identified using the Environment.GetFolderPath method. The number of such folders has doubled since the release of the .NET framwork version 1.1.
Environment Class
In an earlier article I described how you can identify the system folders for the currently logged-in user in order to find their locations. This allows you to find, for example, the user's default folder for application data. You can then store information in this folder, or in a subfolder, making the information part of the user's roaming profile. The previous article described the special folders that were available when using version 1.1 of the .NET framework. Since then, the number of available system folders has more than doubled. This article describes the new directories that can be located when using the .NET framework version 4.0.
The manner for accessing the paths of system folders is unchanged. As before, you call the Environment class' GetFolderPath method, passing a value from the Environment.SpecialFolder enumeration to determine which path to obtain. The results are returned in a string. The new paths are accessed by using extra constant values in the enumeration.
New Enumeration Constants
The list of new constants is shown in the table below, which includes the name of the constant and the result of using it with the GetFolderPath method:
Constant | Description |
---|
AdminTools | Returns the folder that holds administrative tools, including Microsoft Management Console (MMC) consoles that have been modified by the user. This folder is part of the user's roaming profile. |
CDBurning | Returns the path of the folder used to temporarily hold files whilst they are waiting to be written to a CD or DVD. |
CommonAdminTools | Returns the folder that holds administrative tools that are shared by all users of the computer. |
CommonDocuments | Returns the path to the shared documents folder that is accessible by all users. |
CommonMusic | Returns the path to the shared music folder that is accessible by all users. |
CommonOemLinks | This constant is available for backwards compatibility. It returns an empty string. |
CommonPictures | Returns the path to the shared images folder that is accessible by all users. |
CommonStartMenu | Returns the path to the shared Start menu folder that is accessible by all users. Items in this folder appear in the Start menu no matter which user is logged on to Microsoft Windows. |
CommonProgramFilesX86 | Returns the path to the folder that contains 32-bit program files and components that are shared between programs and are available to all users of the computer. |
CommonPrograms | Returns the path to the folder that contains program files and components that are shared between programs and are available to all users of the computer. |
CommonStartup | Returns the path to the Startup folder that is accessible by all users. Shortcuts added to the folder are launched automatically at the end of the logon process. |
CommonDesktopDirectory | Returns the path to the shared Desktop folder that is accessible by all users. Items in this folder appear on the desktop. |
CommonTemplates | Returns the path to the shared templates folder that is shared by all users. This folder is not present for operating systems prior to Windows NT. |
CommonVideos | Returns the path to the shared videos folder that is accessible by all users. |
Fonts | Returns the folder that contains the list of fonts installed on the computer. |
LocalizedResources | Returns the folder that contains resources that are localised for the current user. |
MyDocuments | Introduced in .NET 2.0, this special folder returns the path to the current user's documents folder. This value is equivalent to the "Personal" constant, sharing the same integer value. |
MyVideos | Returns the path to the current user's videos folder. |
NetworkShortcuts | Returns the path of the My Network Shortcuts virtual folder, which can contain links to networked resources, such as remote folders and FTP sites. |
PrinterShortcuts | Returns the path of the Printer Shortcuts folder. This can contain links that appear in the Printers virtual folder. |
ProgramFilesX86 | Returns the path to the Program Files folder that contains the user's 32-bit software. |
Resources | Returns the folder that contains resources that can be used by the current user. |
SystemX86 | Returns the path of the Windows System folder. |
UserProfile | Returns the root folder of the current user's profile. This folder should not be used to store files. Instead, use the path from the ApplicationData special folder. |
Windows | Returns the path of the Windows folder. |
Displaying all Special Folders
The following code loops through a sorted list of special folders, including those covered in the earlier article, outputting the name of the SpecialFolder constant and its current path:
var folders = Enum.GetValues(typeof(Environment.SpecialFolder))
.OfType<Environment.SpecialFolder>().OrderBy(f=>f.ToString());
int maxLength = folders.Max(f=>f.ToString().Length);
foreach (var folder in folders)
{
Console.WriteLine("{0} - {1}", folder.ToString().PadRight(maxLength),
Environment.GetFolderPath(folder));
}
24 March 2012