C# Code Snippet - Access current environment directories and logical drives
(C-Sharp) C# code snippets to access current environment directories such as current directory, system directory and logical drives.
Bookmark:
C# Code Snippet - Access current environment directories and logical drives
These .Net C# code snippets use Environment Class to access the current environment and platform to retrieve information about directories and logical drives. These code snippets shows how to use current environment to get current directory, system directory, temporary directory, home drive, application data directory, windows directory, system root, user profile directory and list all the available logical drives.
// Gets the fully qualified path of the current working directory. Console.WriteLine("Current Directory: " + Environment.CurrentDirectory); // Gets the fully qualified path of the system directory. // Typical value - C:\WINDOWS\system32 Console.WriteLine("System Directory: " + Environment.SystemDirectory); // Retrieves the contents of the specified variable from the environment // block of the calling process. // Gets the temporary directory - %TEMP% or %TMP% Console.WriteLine("GetEnvironment Variable: My temporary directory is " + Environment.GetEnvironmentVariable("TEMP")); // Gets the home drive - %HOMEDRIVE% // Typical value - C: Console.WriteLine("Get Environment Variable: Home Drive " + Environment.GetEnvironmentVariable("HOMEDRIVE")); // Gets Application Data directory - %APPDATA% // Typical value - C:\Documents and Settings\{username}\Application Data Console.WriteLine("Get Environment Variable: Application Data directory " + Environment.GetEnvironmentVariable("APPDATA")); // Gets Windows directory - %WINDIR% // Typical value - C:\WINDOWS Console.WriteLine("Get Environment Variable: Windows directory " + Environment.GetEnvironmentVariable("WINDIR")); // Gets Windows directory - %SYSTEMROOT% // Typical value - The Windows XP root directory, usually C:\WINDOWS Console.WriteLine("Get Environment Variable: System root " + Environment.GetEnvironmentVariable("SYSTEMROOT")); // Gets User Profile directory - %USERPROFILE% // Typical value - C:\Documents and Settings\{username} Console.WriteLine("Get Environment Variable: User profile directory " + Environment.GetEnvironmentVariable("USERPROFILE")); // Returns an array of string containing the names of the logical drives // on the current computer. string[] drives = Environment.GetLogicalDrives(); // Concatenates local drive list Console.WriteLine("Get Logical Drives: " + string.Join(", ", drives));
C# Keywords Used:
- Environment
- Environment.CurrentDirectory
- Environment.SystemDirectory
- Environment.GetEnvironmentVariable
- Environment.GetLogicalDrives
Code Snippet Information:
- Applies To: Visual Studio, .Net, C#, CLI, Environment Variables, Current Directory, System Directory, Temporary Directory, Application Data Directory, Windows Directory, System Root, User Profile Directory, Logical Drives
- Programming Language : C# (C-Sharp)
External Resources:
Ferd :: May 26-2010 :: 08:28 AM
And How we do to know the current open directory path by user in explorer