Getting the Windows Directory




Getting the Windows Directory

Problem

You need to know the full path to the Windows directory.

Solution

Call the GetWinDir method created for your use here to return the Windows directory path in a string:

	public static string GetWinDir()
	{
	    string sysDir = Environment.GetFolderPath(Environment.SpecialFolder.System);
	    return Path.GetFullPath(sysDir + @"\..");
	}

Discussion

There is an enumeration to describe almost every significant operating system folder (Environment.SpecialFolder). But for some reason, since the inception of .NET, the Windows directory has been deemed unacceptably off-limits. This recipe exists for want of an Environment.SpecialFolder.Windows value. Another way to get at this value is to use the Environment.GetEnvironmentVariable function, passing in "windir" as the value.

Figure shows all of the other places in the OS you can get through Environment.SpecialFolder.

The Environment.SpecialFolder enumeration values

Value

Description

ApplicationData

Roaming user's application data directory

CommonApplicationData

Application data directory for all roaming users

CommonProgramFiles

Folder for common application components

Cookies

Directory where cookies are stored

Desktop

The logical desktop folder

DesktopDirectory

The physical desktop folder

Favorites

Where favorites are stored on disk

History

Internet history items folder

InternetCache

Where temporary Internet files are stored

LocalApplicationData

Nonroaming user's application data directory

MyComputer

Physical location of My Computer folder

MyDocuments

Physical location of My Documents folder

MyMusic

Physical location of My Music folder

MyPictures

Physical location of My Pictures folder

Personal

Physical location of personal documents

ProgramFiles

Physical location of the official Program Files directory

Programs

Folder where Program Groups are stored

Recent

Folder containing most recently used documents

SendTo

Send To menu item folder

StartMenu

Folder containing the Start menu structure and items

Startup

Folder corresponding to the Startup Program Group location

System

The system directory

Templates

Common location for document templates


See Also

See the "Environment.SpecialFolder Enumeration" and "Environment.GetFolderPath Method" topics in the MSDN documentation.