C++/CLI Code Snippet - Access current environment directories and logical drives
C++/CLI code snippets to access current environment directories such as current directory, system directory and logical drives.
Bookmark:
C++/CLI Code Snippet - Access current environment directories and logical drives
These .Net C++/CLI 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. array<System::String^> ^drives = Environment::GetLogicalDrives(); // Concatenates local drive list Console::WriteLine("Get Logical Drives: " + System::String::Join(", ", drives));
C++/CLI 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++/CLI
External Resources: