Environment::GetEnvironmentVariable Method (String^)
Retrieves the value of an environment variable from the current process.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- variable
-
Type:
System::String^
The name of the environment variable.
Return Value
Type: System::String^The value of the environment variable specified by variable, or null if the environment variable is not found.
| Exception | Condition |
|---|---|
| ArgumentNullException | variable is null. |
| SecurityException | The caller does not have the required permission to perform this operation. |
The GetEnvironmentVariable(String^) method retrieves an environment variable from the environment block of the current process only. It is equivalent to calling the GetEnvironmentVariable(String^, EnvironmentVariableTarget) method with a target value of EnvironmentVariableTarget::Process. The environment block of the current process includes the following environment variables:
All per-machine environment variables that are defined at the time the process is created, along with their values.
All per-user environment variables that are defined at the time the process is created, along with their values.
Any variables added to the process block while the process is running by calling either the SetEnvironmentVariable(String^, String^) method or the SetEnvironmentVariable(String^, String^, EnvironmentVariableTarget) method with a target value of EnvironmentVariableTarget::Process.
If environment variables are created after the process has started, you can use this method to retrieve only those variables that were created by calling the SetEnvironmentVariable(String^, String^) method or the SetEnvironmentVariable(String^, String^, EnvironmentVariableTarget) method with a target value of .EnvironmentVariableTarget::Process.
To retrieve all environment variables along with their values, call the GetEnvironmentVariables method.
Environment variable names are not case-sensitive.
The following example uses the GetEnvironmentVariable method to retrieve the windir environment variable, which contains the path of the Windows directory.
using namespace System; using namespace System::IO; void main() { // Change the directory to %WINDIR% Environment::CurrentDirectory = Environment::GetEnvironmentVariable( "windir" ); DirectoryInfo^ info = gcnew DirectoryInfo( "." ); Console::WriteLine("Directory Info: {0}", info->FullName); } // The example displays output like the following: // Directory Info: C:\windows
The following example attempts to retrieve the value of an environment variable named Test1 from the process environment block. If the variable doesn't exist, the example creates its and retrieves its value. The example displays the value of the variable. If the example created the variable, it also calls the GetEnvironmentVariables(EnvironmentVariableTarget) method with each member of the EnvironmentVariableTarget enumeration to establish that the variable can be retrieved only from the current process environment block. Finally, if the example created the variable, it deletes it.
for the ability to read the value of variable. Associated enumeration: EnvironmentPermissionAccess::Read
Available since 10
.NET Framework
Available since 1.1