Diese Dokumentation wurde archiviert und wird nicht länger gepflegt.

Environment.SetEnvironmentVariable-Methode (String, String, EnvironmentVariableTarget)

Aktualisiert: November 2007

Erstellt, ändert oder löscht eine Umgebungsvariable, die im aktuellen Prozess oder im Registrierungsschlüssel des Windows-Betriebssystems gespeichert ist, der für den aktuellen Benutzer oder den lokalen Computer reserviert ist.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static void SetEnvironmentVariable(
	string variable,
	string value,
	EnvironmentVariableTarget target
)
public static void SetEnvironmentVariable(
	String variable,
	String value,
	EnvironmentVariableTarget target
)
public static function SetEnvironmentVariable(
	variable : String, 
	value : String, 
	target : EnvironmentVariableTarget
)

Parameter

variable
Typ: System.String
Der Name einer Umgebungsvariablen.
value
Typ: System.String
Ein Wert, der variable zugewiesen werden soll.
target
Typ: System.EnvironmentVariableTarget
Ein EnvironmentVariableTarget-Wert.

AusnahmeBedingung
ArgumentNullException

Der variable-Parameter ist null.

ArgumentException

variable enthält eine Zeichenfolge der Länge 0 (null), ein anfängliches Hexadezimalzeichen 0 (0x00) oder ein Gleichheitszeichen ("=").

– oder –

Die Länge von variable ist größer oder gleich 32.767 Zeichen.

– oder –

target ist kein Member der EnvironmentVariableTarget-Enumeration.

– oder –

target ist Machine oder User, und die Länge von variable ist größer oder gleich 255.

– oder –

target ist Process, und die Länge von value ist größer oder gleich 32.767 Zeichen.

– oder –

Während der Ausführung dieser Operation ist ein Fehler aufgetreten.

NotSupportedException

target ist User oder Machine, und das aktuelle Betriebssystem ist Windows 95, Windows 98 oder Windows Me.

SecurityException

Der Aufrufer verfügt nicht über die erforderliche Berechtigung für diesen Vorgang.

Wenn der value-Parameter nicht leer ist und die durch den variable-Parameter angegebene Umgebungsvariable nicht vorhanden ist, wird die Umgebungsvariable erstellt und dem Inhalt von value zugewiesen. Ausschließlich für die Zwecke dieses Vorgangs wird value als leer betrachtet, wenn es null ist, eine Zeichenfolge von der Länge 0 oder ein anfängliches Hexadezimalzeichen 0 (0x00) enthält.

Wenn variable ein nicht anfängliches Hexadezimalzeichen 0 (null) enthält, werden die Zeichen vor dem Zeichen 0 dem Umgebungsvariablennamen zugewiesen und alle folgenden Zeichen ignoriert.

Wenn value ein nicht anfängliches Hexadezimalzeichen 0 (null) enthält, werden die Zeichen vor dem Zeichen 0 der Umgebungsvariablen zugewiesen und alle folgenden Zeichen ignoriert.

Wenn value leer ist und die von variable genannte Umgebungsvariable vorhanden ist, wird die Umgebungsvariable gelöscht. Wenn variable nicht vorhanden ist, tritt kein Fehler auf, obwohl die Operation nicht ausgeführt werden kann. Vorsicht ist geboten, wenn target Machine ist, weil Sie eine Umgebungsvariable löschen können, die den gesamten lokalen Computer und nicht nur den aktuellen Prozess oder Benutzer betrifft.

Wenn der target-Parameter nicht Process ist, wird die Umgebungsvariable nicht automatisch in den aktuellen Prozess kopiert. Wenn target User ist, wird die Umgebungsvariable in dem Registrierungsschlüssel des Windows-Betriebssystems gespeichert, der für den aktuellen Benutzer reserviert ist, und in den aktuellen Prozess kopiert, wenn der Benutzer, der die Variable gespeichert hat, einen neuen Prozess startet. Wenn target Machine ist, wird die Umgebungsvariable in dem Registrierungsschlüssel des Windows-Betriebssystems gespeichert, der für den lokalen Computer reserviert ist, und in den aktuellen Prozess kopiert, wenn ein beliebiger Benutzer auf dem lokalen Computer einen neuen Prozess startet.

Wenn target User oder Machine ist, werden andere Anwendung mit einer Windows-WM_SETTINGCHANGE-Meldung über den Set-Vorgang benachrichtigt.

Wenn target User oder Machine ist, wird empfohlen, dass die Länge von value weniger als 2048 Zeichen beträgt.

Im folgenden Codebeispiel werden Umgebungsvariablen für die Ziele Process, User und Machine erstellt. Dann wird überprüft, ob die Registrierung des Betriebssystems die Umgebungsvariablen User und Machine enthält, und die Umgebungsvariablen werden gelöscht.

// This example demonstrates the 
//     Environment.GetEnvironmentVariable,
//     Environment.SetEnvironmentVariable, and 
//     Environment.GetEnvironmentVariables overloaded methods.

using System;
using System.Collections;
using Microsoft.Win32;

class Sample 
{
//-------------------------------------------------------------------------------------
// Globals: 
//-------------------------------------------------------------------------------------
    protected static string fmtNameValue = "  {0} {1}.";
    protected static string myVarSuffix = "_GETSET_ENVAR_SAMPLE";

// Four relatively unique environment variable names.
    protected static string myVarA = "A" + myVarSuffix; // default process
    protected static string myVarB = "B" + myVarSuffix; // Current Process
    protected static string myVarC = "C" + myVarSuffix; // Current User
    protected static string myVarD = "D" + myVarSuffix; // Local Machine
//=====================================================================================
// EachVariable: 
// Test whether a specific environment variable exists in a target.
// This section demonstrates Environment.GetEnvironmentVariable.
//-------------------------------------------------------------------------------------
    protected static void EachVariable(string var, EnvironmentVariableTarget tgt)
    {
    string str;
    //
    if (0 == tgt)          // Zero means use the default target.
        str = Environment.GetEnvironmentVariable(var);
    else
        str = Environment.GetEnvironmentVariable(var, tgt);
    Console.WriteLine(fmtNameValue, 
                      var, (String.IsNullOrEmpty(str) ? "doesn't exist" : str));
    }
//-------------------------------------------------------------------------------------
// CheckEachVariable: 
// Uses EachVariable to test whether each environment variable exists in a target.
//-------------------------------------------------------------------------------------
    protected static void CheckEachVariable()
    {
    Console.WriteLine("Process:");
    EachVariable(myVarA, 0);  // Check the default target (current process)
    EachVariable(myVarB, EnvironmentVariableTarget.Process);
    EachVariable(myVarC, EnvironmentVariableTarget.Process);
    EachVariable(myVarD, EnvironmentVariableTarget.Process);
    Console.WriteLine();

    Console.WriteLine("User:");
    EachVariable(myVarA, EnvironmentVariableTarget.User);
    EachVariable(myVarB, EnvironmentVariableTarget.User);
    EachVariable(myVarC, EnvironmentVariableTarget.User);
    EachVariable(myVarD, EnvironmentVariableTarget.User);
    Console.WriteLine();

    Console.WriteLine("Machine:");
    EachVariable(myVarA, EnvironmentVariableTarget.Machine);
    EachVariable(myVarB, EnvironmentVariableTarget.Machine);
    EachVariable(myVarC, EnvironmentVariableTarget.Machine);
    EachVariable(myVarD, EnvironmentVariableTarget.Machine);
    Console.WriteLine();
    }
//=====================================================================================
// AllVariables: CheckAllVariables helper function.
// This section demonstrates Environment.GetEnvironmentVariables.
//-------------------------------------------------------------------------------------
    private static void AllVariables(EnvironmentVariableTarget tgt)
    {
    string value;
    string key;

    foreach(DictionaryEntry de in Environment.GetEnvironmentVariables(tgt))
        {
        key   = (string)de.Key;
        value = (string)de.Value;
        if (key.Contains(myVarSuffix))
            Console.WriteLine(fmtNameValue, key, value);
        }
    Console.WriteLine();
    }
//=====================================================================================
// CheckAllVariables: 
// Uses AllVariables to test whether each environment variable exists in a target.
//-------------------------------------------------------------------------------------
    protected static void CheckAllVariables()
    {
    Console.WriteLine("Process:");
    AllVariables(EnvironmentVariableTarget.Process);

    Console.WriteLine("User:");
    AllVariables(EnvironmentVariableTarget.User);

    Console.WriteLine("Machine:");
    AllVariables(EnvironmentVariableTarget.Machine);
    }
//=====================================================================================
// ChkReg: CheckRegistry helper function.
// This function filters out irrelevant environment variables. 
//-------------------------------------------------------------------------------------
    private static void ChkReg(RegistryKey rk)
    {
    bool exists = false;
    string registryNone = "  Environment variable doesn't exist.";

    foreach (string s in rk.GetValueNames())
        {
        if (s.Contains(myVarSuffix))
            {
            Console.WriteLine(fmtNameValue, s, (string)rk.GetValue(s));
            exists = true;
            }
        }
    if (exists == false)
        Console.WriteLine(registryNone);
    Console.WriteLine();
    }
//-------------------------------------------------------------------------------------
// CheckRegistry: 
// Uses ChkReg to display the User and Machine environment variables in the registry.
//-------------------------------------------------------------------------------------
    protected static void CheckRegistry()
    {
    string subkeyU = @"Environment";
    string subkeyM = @"System\CurrentControlSet\Control\Session Manager\Environment";
    string fmtSubkey = "\"{0}\" key:";

    Console.WriteLine(fmtSubkey, subkeyU);
    ChkReg(Registry.CurrentUser.OpenSubKey(subkeyU));

    Console.WriteLine(fmtSubkey, subkeyM);
    ChkReg(Registry.LocalMachine.OpenSubKey(subkeyM));
    }
//=====================================================================================
// Main:
//-------------------------------------------------------------------------------------
    public static void Main() 
    {
//-------------------------------------------------------------------------------------
// Environment variable values
//-------------------------------------------------------------------------------------
    string existsA = "exists in the default target (Process)";
    string existsB = "exists in Process";
    string existsC = "exists in User";
    string existsD = "exists in Machine";
//-------------------------------------------------------------------------------------
// Messages:
//-------------------------------------------------------------------------------------
    string msg1  = "Step 1:\n" +
                       "  Check whether the environment variables already exist in \n" + 
                       "  the various targets...\n";
    string msg2  = "Step 2:\n" +
                       "  Set the environment variable for each target...\n";
    string msg31 = "Step 3, part 1:\n" + 
                       "  Display the environment variables in each target...\n";
    string msg32 = "Step 3, part 2:\n" +
                       "  Check whether the User and Machine environment variables \n" +
                       "  were created in the Windows operating system registry...\n";
    string msg41 = "Step 4, part 1:\n" +
                       "  Delete the environment variables created for this sample...\n";
    string msg42 = "Step 4, part 2:\n" +
                       "  Check whether the environment variables were deleted \n" +
                       "  in each target...\n";
    string msg43 = "Step 4, part 3:\n" + 
                       "  Check whether the User and Machine environment variables \n" +
                       "  were deleted from the Windows operating system registry...\n";
    string fmt2x   = "  {0,9}: Set {1} = \"{2}\"";
//-------------------------------------------------------------------------------------
// Step 1:
// Check whether the sample environment variables already exist.
// WARNING: These variables will be deleted at the end of this sample.
//-------------------------------------------------------------------------------------
    Console.WriteLine(msg1);
    CheckEachVariable();
    Console.WriteLine();
//-------------------------------------------------------------------------------------
// Step 2:
// Set the environment variable for each target.
// This section demonstrates Environment.SetEnvironmentVariable.
//-------------------------------------------------------------------------------------
    Console.WriteLine(msg2);
// Set the environment variable for the default target (the current process).
    Console.WriteLine(fmt2x, "(default)", myVarA, existsA);
    Environment.SetEnvironmentVariable(myVarA, existsA);

// Set the environment variable for the the current process.
    Console.WriteLine(fmt2x, "Process", myVarB, existsB);
    Environment.SetEnvironmentVariable(myVarB, existsB, 
        EnvironmentVariableTarget.Process);

// Set the environment variable for the the current user.
    Console.WriteLine(fmt2x, "User", myVarC, existsC);
    Environment.SetEnvironmentVariable(myVarC, existsC, 
        EnvironmentVariableTarget.User);

// Set the environment variable for the the local machine.
    Console.WriteLine(fmt2x, "Machine", myVarD, existsD);
    Environment.SetEnvironmentVariable(myVarD, existsD, 
        EnvironmentVariableTarget.Machine);
    Console.WriteLine();
//-------------------------------------------------------------------------------------
// Step 3, part 1:
// Display the environment variables in each target.
//-------------------------------------------------------------------------------------
    Console.WriteLine(msg31);
    CheckAllVariables();
    Console.WriteLine();
//-------------------------------------------------------------------------------------
// Step 3, part 2:
// Check whether the User and Machine environment variables were created in the Windows 
// operating system registry.
//-------------------------------------------------------------------------------------
    Console.WriteLine(msg32);
    CheckRegistry();
    Console.WriteLine();
//-------------------------------------------------------------------------------------
// Step 4, part 1:
// Delete the environment variables created for this sample.
// This section demonstrates using Environment.SetEnvironmentVariable to delete an 
// environment variable.
//-------------------------------------------------------------------------------------
    Console.WriteLine(msg41);
    Environment.SetEnvironmentVariable(myVarA, null);
    Environment.SetEnvironmentVariable(myVarB, null, EnvironmentVariableTarget.Process);
    Environment.SetEnvironmentVariable(myVarC, null, EnvironmentVariableTarget.User);
    Environment.SetEnvironmentVariable(myVarD, null, EnvironmentVariableTarget.Machine);
//-------------------------------------------------------------------------------------
// Step 4, part 2:
// Check whether the environment variables were deleted in each target.
//-------------------------------------------------------------------------------------
    Console.WriteLine(msg42);
    CheckEachVariable();
//-------------------------------------------------------------------------------------
// Step 4, part 3:
// Check whether the User and Machine environment variables were deleted from the 
// Windows operating system registry.
//-------------------------------------------------------------------------------------
    Console.WriteLine(msg43);
    CheckRegistry();
    }
}
/*
This example produces the following results:

Step 1:
  Check whether the environment variables already exist in
  the various targets...

Process:
  A_GETSET_ENVAR_SAMPLE doesn't exist.
  B_GETSET_ENVAR_SAMPLE doesn't exist.
  C_GETSET_ENVAR_SAMPLE doesn't exist.
  D_GETSET_ENVAR_SAMPLE doesn't exist.

User:
  A_GETSET_ENVAR_SAMPLE doesn't exist.
  B_GETSET_ENVAR_SAMPLE doesn't exist.
  C_GETSET_ENVAR_SAMPLE doesn't exist.
  D_GETSET_ENVAR_SAMPLE doesn't exist.

Machine:
  A_GETSET_ENVAR_SAMPLE doesn't exist.
  B_GETSET_ENVAR_SAMPLE doesn't exist.
  C_GETSET_ENVAR_SAMPLE doesn't exist.
  D_GETSET_ENVAR_SAMPLE doesn't exist.


Step 2:
  Set the environment variable for each target...

  (default): Set A_GETSET_ENVAR_SAMPLE = "exists in the default target (Process)"
    Process: Set B_GETSET_ENVAR_SAMPLE = "exists in Process"
       User: Set C_GETSET_ENVAR_SAMPLE = "exists in User"
    Machine: Set D_GETSET_ENVAR_SAMPLE = "exists in Machine"

Step 3, part 1:
  Display the environment variables in each target...

Process:
  B_GETSET_ENVAR_SAMPLE exists in Process.
  A_GETSET_ENVAR_SAMPLE exists in the default target (Process).

User:
  C_GETSET_ENVAR_SAMPLE exists in User.

Machine:
  D_GETSET_ENVAR_SAMPLE exists in Machine.


Step 3, part 2:
  Check whether the User and Machine environment variables
  were created in the Windows operating system registry...

"Environment" key:
  C_GETSET_ENVAR_SAMPLE exists in User.

"System\CurrentControlSet\Control\Session Manager\Environment" key:
  D_GETSET_ENVAR_SAMPLE exists in Machine.


Step 4, part 1:
  Delete the environment variables created for this sample...

Step 4, part 2:
  Check whether the environment variables were deleted
  in each target...

Process:
  A_GETSET_ENVAR_SAMPLE doesn't exist.
  B_GETSET_ENVAR_SAMPLE doesn't exist.
  C_GETSET_ENVAR_SAMPLE doesn't exist.
  D_GETSET_ENVAR_SAMPLE doesn't exist.

User:
  A_GETSET_ENVAR_SAMPLE doesn't exist.
  B_GETSET_ENVAR_SAMPLE doesn't exist.
  C_GETSET_ENVAR_SAMPLE doesn't exist.
  D_GETSET_ENVAR_SAMPLE doesn't exist.

Machine:
  A_GETSET_ENVAR_SAMPLE doesn't exist.
  B_GETSET_ENVAR_SAMPLE doesn't exist.
  C_GETSET_ENVAR_SAMPLE doesn't exist.
  D_GETSET_ENVAR_SAMPLE doesn't exist.

Step 4, part 3:
  Check whether the User and Machine environment variables
  were deleted from the Windows operating system registry...

"Environment" key:
  Environment variable doesn't exist.

"System\CurrentControlSet\Control\Session Manager\Environment" key:
  Environment variable doesn't exist.
*/


// This example demonstrates the 
//     Environment.GetEnvironmentVariable,
//     Environment.SetEnvironmentVariable, and 
//     Environment.GetEnvironmentVariables overloaded methods.
import System.*;
import System.Collections.*;
import Microsoft.Win32.*;

class Sample
{
    //-------------------------------------------------------------------------
    // Globals: 
    //-------------------------------------------------------------------------
    protected static String fmtNameValue = "  {0} {1}.";
    protected static String myVarSuffix = "_GETSET_ENVAR_SAMPLE";

    // Four relatively unique environment variable names.
    protected static String myVarA = "A" + myVarSuffix; // default process
    protected static String myVarB = "B" + myVarSuffix; // Current Process
    protected static String myVarC = "C" + myVarSuffix; // Current User
    protected static String myVarD = "D" + myVarSuffix;

    // Local Machine
    //=========================================================================
    // EachVariable: 
    // Test whether a specific environment variable exists in a target.
    // This section demonstrates Environment.GetEnvironmentVariable.
    //-------------------------------------------------------------------------
    protected static void EachVariable(String var, 
        EnvironmentVariableTarget tgt)
    {
        String str;
        //
        if (tgt.Equals((EnvironmentVariableTarget)0)) {
            // Zero means use the default target.
            str = Environment.GetEnvironmentVariable(var);
        }
        else {
            str = Environment.GetEnvironmentVariable(var, tgt);
        }
        Console.WriteLine(fmtNameValue, var,
            (String.IsNullOrEmpty(str)) ? "doesn't exist" : str);
    } //EachVariable

    //-------------------------------------------------------------------------
    // CheckEachVariable: 
    // Uses EachVariable to test whether each environment variable exists in a
    // target.
    //-------------------------------------------------------------------------
    protected static void CheckEachVariable()
    {
        Console.WriteLine("Process:");
        // Check the default target (current process)
        EachVariable(myVarA, (EnvironmentVariableTarget)0); 
        EachVariable(myVarB, EnvironmentVariableTarget.Process);
        EachVariable(myVarC, EnvironmentVariableTarget.Process);
        EachVariable(myVarD, EnvironmentVariableTarget.Process);
        Console.WriteLine();

        Console.WriteLine("User:");
        EachVariable(myVarA, EnvironmentVariableTarget.User);
        EachVariable(myVarB, EnvironmentVariableTarget.User);
        EachVariable(myVarC, EnvironmentVariableTarget.User);
        EachVariable(myVarD, EnvironmentVariableTarget.User);
        Console.WriteLine();

        Console.WriteLine("Machine:");
        EachVariable(myVarA, EnvironmentVariableTarget.Machine);
        EachVariable(myVarB, EnvironmentVariableTarget.Machine);
        EachVariable(myVarC, EnvironmentVariableTarget.Machine);
        EachVariable(myVarD, EnvironmentVariableTarget.Machine);
        Console.WriteLine();
    } //CheckEachVariable

    //=========================================================================
    // AllVariables: CheckAllVariables helper function.
    // This section demonstrates Environment.GetEnvironmentVariables.
    //-------------------------------------------------------------------------
    private static void AllVariables(EnvironmentVariableTarget tgt)
    {
        String value;
        String key;        
        DictionaryEntry de;
        IEnumerator objEnum = 
            Environment.GetEnvironmentVariables(tgt).GetEnumerator();
        while (objEnum.MoveNext()) {
            de = (DictionaryEntry)objEnum.get_Current();
            key = (System.String)(de.get_Key());
            value = (System.String)(de.get_Value());
            if (key.Contains(myVarSuffix)) {
                Console.WriteLine(fmtNameValue, key, value);
            }
        }
        Console.WriteLine();
    } //AllVariables

    //=========================================================================
    // CheckAllVariables: 
    // Uses AllVariables to test whether each environment variable exists in a
    // target.
    //-------------------------------------------------------------------------
    protected static void CheckAllVariables()
    {
        Console.WriteLine("Process:");
        AllVariables(EnvironmentVariableTarget.Process);

        Console.WriteLine("User:");
        AllVariables(EnvironmentVariableTarget.User);

        Console.WriteLine("Machine:");
        AllVariables(EnvironmentVariableTarget.Machine);
    } //CheckAllVariables

    //=========================================================================
    // ChkReg: CheckRegistry helper function.
    // This function filters out irrelevant environment variables. 
    //-------------------------------------------------------------------------
    private static void ChkReg(RegistryKey rk)
    {
        boolean exists = false;
        String registryNone = "  Environment variable doesn't exist.";
        for (int iCtr = 0; iCtr < rk.GetValueNames().get_Length(); iCtr++) {
            String s = ( String)rk.GetValueNames().get_Item(iCtr);
            if (s.Contains(myVarSuffix)) {
                Console.WriteLine(fmtNameValue, s,
                    (System.String)(rk.GetValue(s)));
                exists = true;
            }
        }
        if (exists == false) {
            Console.WriteLine(registryNone);
        }
        Console.WriteLine();
    } //ChkReg

    //-------------------------------------------------------------------------
    // CheckRegistry: 
    // Uses ChkReg to display the User and Machine environment variables in the
    // registry.
    //-------------------------------------------------------------------------
    protected static void CheckRegistry()
    {
        String subkeyU = "Environment";
        String subkeyM = 
            "System\\CurrentControlSet\\Control\\Session Manager\\Environment";
        String fmtSubkey = "\"{0}\" key:";

        Console.WriteLine(fmtSubkey, subkeyU);
        ChkReg(Registry.CurrentUser.OpenSubKey(subkeyU));

        Console.WriteLine(fmtSubkey, subkeyM);
        ChkReg(Registry.LocalMachine.OpenSubKey(subkeyM));
    } //CheckRegistry

    //=========================================================================
    // main:
    //-------------------------------------------------------------------------
    public static void main(String[] args)
    {
        //---------------------------------------------------------------------
        // Environment variable values
        //---------------------------------------------------------------------
        String existsA = "exists in the default target (Process)";
        String existsB = "exists in Process";
        String existsC = "exists in User";
        String existsD = "exists in Machine";
        //---------------------------------------------------------------------
        // Messages:
        //---------------------------------------------------------------------
        String msg1 = "Step 1:\n" 
            + "  Check whether the environment variables already exist in \n" 
            + "  the various targets...\n";
        String msg2 = "Step 2:\n" 
            +"  Set the environment variable for each target...\n";
        String msg31 = "Step 3, part 1:\n" 
            +"  Display the environment variables in each target...\n";
        String msg32 = "Step 3, part 2:\n" 
            +"  Check whether the User and Machine environment variables \n" 
            +"  were created in the Windows operating system registry...\n";
        String msg41 = "Step 4, part 1:\n" 
            +"  Delete the environment variables created for this sample...\n";
        String msg42 = "Step 4, part 2:\n" 
            +"  Check whether the environment variables were deleted \n" 
            +"  in each target...\n";
        String msg43 = "Step 4, part 3:\n" 
            +"  Check whether the User and Machine environment variables \n" 
            +"  were deleted from the Windows operating system registry...\n";
        String fmt2x = "  {0,9}: Set {1} = \"{2}\"";
        //---------------------------------------------------------------------
        // Step 1:
        // Check whether the sample environment variables already exist.
        // WARNING: These variables will be deleted at the end of this sample.
        //---------------------------------------------------------------------
        Console.WriteLine(msg1);
        CheckEachVariable();
        Console.WriteLine();
        //---------------------------------------------------------------------
        // Step 2:
        // Set the environment variable for each target.
        // This section demonstrates Environment.SetEnvironmentVariable.
        //---------------------------------------------------------------------
        Console.WriteLine(msg2);

        // Set the environment variable for the default target
        // (the current process).
        Console.WriteLine(fmt2x, "(default)", myVarA, existsA);
        Environment.SetEnvironmentVariable(myVarA, existsA);

        // Set the environment variable for the the current process.
        Console.WriteLine(fmt2x, "Process", myVarB, existsB);
        Environment.SetEnvironmentVariable(myVarB, existsB,
            EnvironmentVariableTarget.Process);

        // Set the environment variable for the the current user.
        Console.WriteLine(fmt2x, "User", myVarC, existsC);
        Environment.SetEnvironmentVariable(myVarC, existsC,
            EnvironmentVariableTarget.User);

        // Set the environment variable for the the local machine.
        Console.WriteLine(fmt2x, "Machine", myVarD, existsD);
        Environment.SetEnvironmentVariable(myVarD, existsD,
            EnvironmentVariableTarget.Machine);
        Console.WriteLine();
        //---------------------------------------------------------------------
        // Step 3, part 1:
        // Display the environment variables in each target.
        //---------------------------------------------------------------------
        Console.WriteLine(msg31);
        CheckAllVariables();
        Console.WriteLine();
        //---------------------------------------------------------------------
        // Step 3, part 2:
        // Check whether the User and Machine environment variables were
        // created in the Windows operating system registry.
        //---------------------------------------------------------------------
        Console.WriteLine(msg32);
        CheckRegistry();
        Console.WriteLine();
        //---------------------------------------------------------------------
        // Step 4, part 1:
        // Delete the environment variables created for this sample.
        // This section demonstrates using Environment.SetEnvironmentVariable
        // to delete an environment variable.
        //---------------------------------------------------------------------       
        Console.WriteLine(msg41);
        Environment.SetEnvironmentVariable(myVarA, null);
        Environment.SetEnvironmentVariable(myVarB, null,
            EnvironmentVariableTarget.Process);
        Environment.SetEnvironmentVariable(myVarC, null,
            EnvironmentVariableTarget.User);
        Environment.SetEnvironmentVariable(myVarD, null,
            EnvironmentVariableTarget.Machine);
        //---------------------------------------------------------------------
        // Step 4, part 2:
        // Check whether the environment variables were deleted in each target.
        //---------------------------------------------------------------------        
        Console.WriteLine(msg42);
        CheckEachVariable();
        //---------------------------------------------------------------------
        // Step 4, part 3:
        // Check whether the User and Machine environment variables were
        // deleted from the Windows operating system registry.
        //---------------------------------------------------------------------
        Console.WriteLine(msg43);
        CheckRegistry();
    } //main
} //Sample
/*
This example produces the following results:

Step 1:
  Check whether the environment variables already exist in
  the various targets...

Process:
  A_GETSET_ENVAR_SAMPLE doesn't exist.
  B_GETSET_ENVAR_SAMPLE doesn't exist.
  C_GETSET_ENVAR_SAMPLE doesn't exist.
  D_GETSET_ENVAR_SAMPLE doesn't exist.

User:
  A_GETSET_ENVAR_SAMPLE doesn't exist.
  B_GETSET_ENVAR_SAMPLE doesn't exist.
  C_GETSET_ENVAR_SAMPLE doesn't exist.
  D_GETSET_ENVAR_SAMPLE doesn't exist.

Machine:
  A_GETSET_ENVAR_SAMPLE doesn't exist.
  B_GETSET_ENVAR_SAMPLE doesn't exist.
  C_GETSET_ENVAR_SAMPLE doesn't exist.
  D_GETSET_ENVAR_SAMPLE doesn't exist.


Step 2:
  Set the environment variable for each target...

  (default): Set A_GETSET_ENVAR_SAMPLE = "exists in the default target (Process)"
    Process: Set B_GETSET_ENVAR_SAMPLE = "exists in Process"
       User: Set C_GETSET_ENVAR_SAMPLE = "exists in User"
    Machine: Set D_GETSET_ENVAR_SAMPLE = "exists in Machine"

Step 3, part 1:
  Display the environment variables in each target...

Process:
  B_GETSET_ENVAR_SAMPLE exists in Process.
  A_GETSET_ENVAR_SAMPLE exists in the default target (Process).

User:
  C_GETSET_ENVAR_SAMPLE exists in User.

Machine:
  D_GETSET_ENVAR_SAMPLE exists in Machine.


Step 3, part 2:
  Check whether the User and Machine environment variables
  were created in the Windows operating system registry...

"Environment" key:
  C_GETSET_ENVAR_SAMPLE exists in User.

"System\CurrentControlSet\Control\Session Manager\Environment" key:
  D_GETSET_ENVAR_SAMPLE exists in Machine.


Step 4, part 1:
  Delete the environment variables created for this sample...

Step 4, part 2:
  Check whether the environment variables were deleted
  in each target...

Process:
  A_GETSET_ENVAR_SAMPLE doesn't exist.
  B_GETSET_ENVAR_SAMPLE doesn't exist.
  C_GETSET_ENVAR_SAMPLE doesn't exist.
  D_GETSET_ENVAR_SAMPLE doesn't exist.

User:
  A_GETSET_ENVAR_SAMPLE doesn't exist.
  B_GETSET_ENVAR_SAMPLE doesn't exist.
  C_GETSET_ENVAR_SAMPLE doesn't exist.
  D_GETSET_ENVAR_SAMPLE doesn't exist.

Machine:
  A_GETSET_ENVAR_SAMPLE doesn't exist.
  B_GETSET_ENVAR_SAMPLE doesn't exist.
  C_GETSET_ENVAR_SAMPLE doesn't exist.
  D_GETSET_ENVAR_SAMPLE doesn't exist.

Step 4, part 3:
  Check whether the User and Machine environment variables
  were deleted from the Windows operating system registry...

"Environment" key:
  Environment variable doesn't exist.

"System\CurrentControlSet\Control\Session Manager\Environment" key:
  Environment variable doesn't exist.
*/


Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

.NET Framework und .NET Compact Framework unterstützen nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen für .NET Framework.

.NET Framework

Unterstützt in: 3.5, 3.0, 2.0
Anzeigen: