Get Special Folder Location using System.Environment in C#


System Special Folders

System folders are folders such as “Program Files”, “Start Menu” or “System”, which are used to store common information. These folders are set by default by the system, in some cases explicitly defined by the user, when installing operating system. The locations of these directories can be different for different operating systems, locations are localized and user can change some of the locations.

Environment.SpecialFolder enumeration in System namespace defines enumerated constants used to retrieve directory paths to system special folders.

Some of the constants defined in Environment.SpecialFolder enumeration are list below. For a complete list, please refer to Microsft.NET documentation.

Folder Name

Explanation

ProgramFiles

The program files folder where programs are usually installed.

CommonProgramFiles

The Common Files folder of Program Files

DeskTopDirectory

The folder representing the desktop of the User.

Favorites

The favorite’s folder to store favorite’s link.

History

The history folder to store history files

Personal

The MyDocuments folder.

Programs

The folder representing Programs Menu in the Start Menu.

StartMenu

The folder of the start menu.

Recent

The recent folder.

SendTo

The Send To folder

System

The System folder of the machine.

ApplicationData

The application data folder.

CommonApplicationData

The Common Application Data Folder.

LocalApplicationData

The Local Application Data Folder.

Cookies

The folder used to store cookies settings.

StartUp

The startUp Menu Folder on the start >> programs folder.

 

Getting Folder Location

The GetFolderPath static method of Environment class returns the locations associated with SpecialFolder enumeration.

The overloaded GetFolderPath takes SpecialFolderOption enum as additional parameter.

SpecialFolderOption has following enumeration constants.

  • Create - Path of the folder is created if it not existing.
  • DoNotVerify  - Just returns path, existence of path is not verified. Useful in case of network paths, reduces lag time
  • None - Path of the folder is verified, if exists path is returned, if not exists empty string is returned.

The following code snippet demonstrates how we can get the information related to the folders mentioned in the above table.

// Get Program Files location.
string programfiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);

// Get Common Program Files location.
string commonProgramfiles = Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles, Environment.SpecialFolderOption.None);