C# Environment GetEnvironmentVariable(String, EnvironmentVariableTarget)

Description

Environment GetEnvironmentVariable(String, EnvironmentVariableTarget) retrieves the value of an environment variable from the current process or from the Windows operating system registry key for the current user or local machine.

Syntax

Environment.GetEnvironmentVariable(String, EnvironmentVariableTarget) has the following syntax.


public static string GetEnvironmentVariable(
  string variable,
  EnvironmentVariableTarget target
)

Parameters

Environment.GetEnvironmentVariable(String, EnvironmentVariableTarget) has the following parameters.

  • variable - The name of an environment variable.
  • target - One of the EnvironmentVariableTarget values.

Returns

Environment.GetEnvironmentVariable(String, EnvironmentVariableTarget) method returns The value of the environment variable specified by the variable and target parameters, or null if the environment variable is not found.

Example

The following example creates environment variables for the Process, User, and Machine targets, checks whether the operating system registry contains the User and Machine environment variables, then deletes the environment variables.


//from   www . ja  v a  2  s . c  o  m
using System;
using System.Collections;
using Microsoft.Win32;

class Sample 
{
  public static void Main(){
        string str = Environment.GetEnvironmentVariable("A_GETSET_ENVAR_SAMPLE");
        Console.WriteLine(str);
  }    
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version