Environment Klas

Definitie

Biedt informatie over en middelen voor het manipuleren van de huidige omgeving en het huidige platform. Deze klasse kan niet worden overgenomen.

public ref class Environment abstract sealed
public ref class Environment sealed
public static class Environment
public sealed class Environment
[System.Runtime.InteropServices.ComVisible(true)]
public static class Environment
type Environment = class
[<System.Runtime.InteropServices.ComVisible(true)>]
type Environment = class
Public Class Environment
Public NotInheritable Class Environment
Overname
Environment
Kenmerken

Voorbeelden

In het volgende voorbeeld wordt een lijst met informatie over de huidige omgeving weergegeven.

// Sample for Environment class summary
using System;
using System.Collections;

class Sample
{
    public static void Main()
    {
        string str;
        string nl = Environment.NewLine;
        //
        Console.WriteLine();
        Console.WriteLine("-- Environment members --");

        //  Invoke this sample with an arbitrary set of command line arguments.
        Console.WriteLine("CommandLine: {0}", Environment.CommandLine);

        string[] arguments = Environment.GetCommandLineArgs();
        Console.WriteLine("GetCommandLineArgs: {0}", String.Join(", ", arguments));

        //  <-- Keep this information secure! -->
        Console.WriteLine("CurrentDirectory: {0}", Environment.CurrentDirectory);

        Console.WriteLine("ExitCode: {0}", Environment.ExitCode);

        Console.WriteLine("HasShutdownStarted: {0}", Environment.HasShutdownStarted);

        //  <-- Keep this information secure! -->
        Console.WriteLine("MachineName: {0}", Environment.MachineName);

        Console.WriteLine("NewLine: {0}  first line{0}  second line{0}  third line",
                              Environment.NewLine);

        Console.WriteLine("OSVersion: {0}", Environment.OSVersion.ToString());

        Console.WriteLine("StackTrace: '{0}'", Environment.StackTrace);

        //  <-- Keep this information secure! -->
        Console.WriteLine("SystemDirectory: {0}", Environment.SystemDirectory);

        Console.WriteLine("TickCount: {0}", Environment.TickCount);

        //  <-- Keep this information secure! -->
        Console.WriteLine("UserDomainName: {0}", Environment.UserDomainName);

        Console.WriteLine("UserInteractive: {0}", Environment.UserInteractive);

        //  <-- Keep this information secure! -->
        Console.WriteLine("UserName: {0}", Environment.UserName);

        Console.WriteLine("Version: {0}", Environment.Version.ToString());

        Console.WriteLine("WorkingSet: {0}", Environment.WorkingSet);

        //  No example for Exit(exitCode) because doing so would terminate this example.

        //  <-- Keep this information secure! -->
        string query = "My system drive is %SystemDrive% and my system root is %SystemRoot%";
        str = Environment.ExpandEnvironmentVariables(query);
        Console.WriteLine("ExpandEnvironmentVariables: {0}  {1}", nl, str);

        Console.WriteLine("GetEnvironmentVariable: {0}  My temporary directory is {1}.", nl,
                               Environment.GetEnvironmentVariable("TEMP"));

        Console.WriteLine("GetEnvironmentVariables: ");
        IDictionary environmentVariables = Environment.GetEnvironmentVariables();
        foreach (DictionaryEntry de in environmentVariables)
        {
            Console.WriteLine("  {0} = {1}", de.Key, de.Value);
        }

        Console.WriteLine("GetFolderPath: {0}",
                     Environment.GetFolderPath(Environment.SpecialFolder.System));

        string[] drives = Environment.GetLogicalDrives();
        Console.WriteLine("GetLogicalDrives: {0}", String.Join(", ", drives));
    }
}
/*
This example produces results similar to the following:
(Any result that is lengthy or reveals information that should remain
secure has been omitted and marked "!---OMITTED---!".)

C:\>env0 ARBITRARY TEXT

-- Environment members --
CommandLine: env0 ARBITRARY TEXT
GetCommandLineArgs: env0, ARBITRARY, TEXT
CurrentDirectory: C:\Documents and Settings\!---OMITTED---!
ExitCode: 0
HasShutdownStarted: False
MachineName: !---OMITTED---!
NewLine:
  first line
  second line
  third line
OSVersion: Microsoft Windows NT 5.1.2600.0
StackTrace: '   at System.Environment.GetStackTrace(Exception e)
   at System.Environment.GetStackTrace(Exception e)
   at System.Environment.get_StackTrace()
   at Sample.Main()'
SystemDirectory: C:\WINNT\System32
TickCount: 17995355
UserDomainName: !---OMITTED---!
UserInteractive: True
UserName: !---OMITTED---!
Version: !---OMITTED---!
WorkingSet: 5038080
ExpandEnvironmentVariables:
  My system drive is C: and my system root is C:\WINNT
GetEnvironmentVariable:
  My temporary directory is C:\DOCUME~1\!---OMITTED---!\LOCALS~1\Temp.
GetEnvironmentVariables:
  !---OMITTED---!
GetFolderPath: C:\WINNT\System32
GetLogicalDrives: A:\, C:\, D:\

*/
// Sample for Environment class summary
open System
open System.Collections

let nl = Environment.NewLine

printfn ""
printfn "-- Environment members --"

//  Invoke this sample with an arbitrary set of command line arguments.
printfn $"CommandLine: {Environment.CommandLine}"

Environment.GetCommandLineArgs()
|> String.concat ", "
|> printfn "GetCommandLineArgs: %s"

//  <-- Keep this information secure! -->
printfn $"CurrentDirectory: {Environment.CurrentDirectory}"

printfn $"ExitCode: {Environment.ExitCode}"

printfn $"HasShutdownStarted: {Environment.HasShutdownStarted}"

//  <-- Keep this information secure! -->
printfn $"MachineName: {Environment.MachineName}"

printfn $"NewLine: {nl}  first line{nl}  second line{nl}  third line"

printfn $"OSVersion: {Environment.OSVersion}"

printfn $"StackTrace: '{Environment.StackTrace}'"

//  <-- Keep this information secure! -->
printfn $"SystemDirectory: {Environment.SystemDirectory}"

printfn $"TickCount: {Environment.TickCount}"

//  <-- Keep this information secure! -->
printfn $"UserDomainName: {Environment.UserDomainName}"

printfn $"UserInteractive: {Environment.UserInteractive}"

//  <-- Keep this information secure! -->
printfn $"UserName: {Environment.UserName}"

printfn $"Version: {Environment.Version}"

printfn $"WorkingSet: {Environment.WorkingSet}"

//  No example for Exit(exitCode) because doing so would terminate this example.

//  <-- Keep this information secure! -->
let query = "My system drive is %SystemDrive% and my system root is %SystemRoot%"
let str = Environment.ExpandEnvironmentVariables query
printfn $"ExpandEnvironmentVariables: {nl}  {str}"

printfn $"""GetEnvironmentVariable: {nl}  My temporary directory is {Environment.GetEnvironmentVariable "TEMP"}."""

printfn "GetEnvironmentVariables: "
let environmentVariables = Environment.GetEnvironmentVariables()
for de in environmentVariables do
    let de = de :?> DictionaryEntry
    printfn $"  {de.Key} = {de.Value}"

printfn $"GetFolderPath: {Environment.GetFolderPath Environment.SpecialFolder.System}"

Environment.GetLogicalDrives()
|> String.concat ", "
|> printfn "GetLogicalDrives: %s"

// This example produces results similar to the following:
//     (Any result that is lengthy or reveals information that should remain
//     secure has been omitted and marked "!---OMITTED---!".)
//     
//     C:\>env0 ARBITRARY TEXT
//     
//     -- Environment members --
//     CommandLine: env0 ARBITRARY TEXT
//     GetCommandLineArgs: env0, ARBITRARY, TEXT
//     CurrentDirectory: C:\Documents and Settings\!---OMITTED---!
//     ExitCode: 0
//     HasShutdownStarted: False
//     MachineName: !---OMITTED---!
//     NewLine:
//       first line
//       second line
//       third line
//     OSVersion: Microsoft Windows NT 5.1.2600.0
//     StackTrace: '   at System.Environment.GetStackTrace(Exception e)
//        at System.Environment.GetStackTrace(Exception e)
//        at System.Environment.get_StackTrace()
//        at Sample.Main()'
//     SystemDirectory: C:\WINNT\System32
//     TickCount: 17995355
//     UserDomainName: !---OMITTED---!
//     UserInteractive: True
//     UserName: !---OMITTED---!
//     Version: !---OMITTED---!
//     WorkingSet: 5038080
//     ExpandEnvironmentVariables:
//       My system drive is C: and my system root is C:\WINNT
//     GetEnvironmentVariable:
//       My temporary directory is C:\DOCUME~1\!---OMITTED---!\LOCALS~1\Temp.
//     GetEnvironmentVariables:
//       !---OMITTED---!
//     GetFolderPath: C:\WINNT\System32
//     GetLogicalDrives: A:\, C:\, D:\
' Sample for Environment class summary
Imports System.Collections

Class Sample
   Public Shared Sub Main()
      Dim str As [String]
      Dim nl As [String] = Environment.NewLine
      '
      Console.WriteLine()
      Console.WriteLine("-- Environment members --")
      
      '  Invoke this sample with an arbitrary set of command line arguments.
      Console.WriteLine("CommandLine: {0}", Environment.CommandLine)
      
      Dim arguments As [String]() = Environment.GetCommandLineArgs()
      Console.WriteLine("GetCommandLineArgs: {0}", [String].Join(", ", arguments))
      
      '  <-- Keep this information secure! -->
      Console.WriteLine("CurrentDirectory: {0}", Environment.CurrentDirectory)
      
      Console.WriteLine("ExitCode: {0}", Environment.ExitCode)
      
      Console.WriteLine("HasShutdownStarted: {0}", Environment.HasShutdownStarted)
      
      '  <-- Keep this information secure! -->
      Console.WriteLine("MachineName: {0}", Environment.MachineName)
      
      Console.WriteLine("NewLine: {0}  first line{0}  second line{0}" & _
                        "  third line", Environment.NewLine)
      
      Console.WriteLine("OSVersion: {0}", Environment.OSVersion.ToString())
      
      Console.WriteLine("StackTrace: '{0}'", Environment.StackTrace)
      
      '  <-- Keep this information secure! -->
      Console.WriteLine("SystemDirectory: {0}", Environment.SystemDirectory)
      
      Console.WriteLine("TickCount: {0}", Environment.TickCount)
      
      '  <-- Keep this information secure! -->
      Console.WriteLine("UserDomainName: {0}", Environment.UserDomainName)
      
      Console.WriteLine("UserInteractive: {0}", Environment.UserInteractive)
      
      '  <-- Keep this information secure! -->
      Console.WriteLine("UserName: {0}", Environment.UserName)
      
      Console.WriteLine("Version: {0}", Environment.Version.ToString())
      
      Console.WriteLine("WorkingSet: {0}", Environment.WorkingSet)
      
      '  No example for Exit(exitCode) because doing so would terminate this example.

      '  <-- Keep this information secure! -->
      Dim query As [String] = "My system drive is %SystemDrive% and my" & _
                              " system root is %SystemRoot%"
      str = Environment.ExpandEnvironmentVariables(query)
      Console.WriteLine("ExpandEnvironmentVariables: {0}  {1}", nl, str)
      
      Console.WriteLine("GetEnvironmentVariable: {0}  My temporary directory is {1}.", _
                        nl, Environment.GetEnvironmentVariable("TEMP"))
      
      Console.WriteLine("GetEnvironmentVariables: ")
      Dim environmentVariables As IDictionary = Environment.GetEnvironmentVariables()
      Dim de As DictionaryEntry
      For Each de In environmentVariables
         Console.WriteLine("  {0} = {1}", de.Key, de.Value)
      Next de
      
      Console.WriteLine("GetFolderPath: {0}", _
              Environment.GetFolderPath(Environment.SpecialFolder.System))
      
      Dim drives As [String]() = Environment.GetLogicalDrives()
      Console.WriteLine("GetLogicalDrives: {0}", [String].Join(", ", drives))
   End Sub
End Class
'
'This example produces results similar to the following:
'(Any result that is lengthy or reveals information that should remain 
'secure has been omitted and marked "!---OMITTED---!".)
'
'C:\>env0 ARBITRARY TEXT
'
'-- Environment members --
'CommandLine: env0 ARBITRARY TEXT
'GetCommandLineArgs: env0, ARBITRARY, TEXT
'CurrentDirectory: C:\Documents and Settings\!---OMITTED---!
'ExitCode: 0
'HasShutdownStarted: False
'MachineName: !---OMITTED---!
'NewLine:
'  first line
'  second line
'  third line
'OSVersion: Microsoft Windows NT 5.1.2600.0
'StackTrace: '   at System.Environment.GetStackTrace(Exception e)
'   at System.Environment.GetStackTrace(Exception e)
'   at System.Environment.get_StackTrace()
'   at Sample.Main()'
'SystemDirectory: C:\WINNT\System32
'TickCount: 17995355
'UserDomainName: !---OMITTED---!
'UserInteractive: True
'UserName: !---OMITTED---!
'Version: !---OMITTED---!
'WorkingSet: 5038080
'ExpandEnvironmentVariables:
'  My system drive is C: and my system root is C:\WINNT
'GetEnvironmentVariable:
'  My temporary directory is C:\DOCUME~1\!---OMITTED---!\LOCALS~1\Temp.
'GetEnvironmentVariables: 
'  !---OMITTED---!
'GetFolderPath: C:\WINNT\System32
'GetLogicalDrives: A:\, C:\, D:\
'

Opmerkingen

Gebruik de Environment klasse om informatie op te halen, zoals opdrachtregelargumenten, de afsluitcode, instellingen voor omgevingsvariabelen, de inhoud van de aanroepstack, de tijd sinds het laatste systeem opstarten en de versie van de algemene taalruntime.

Eigenschappen

Name Description
CommandLine

Hiermee haalt u de opdrachtregel voor dit proces op.

CpuUsage

Haal het CPU-gebruik op, inclusief de procestijd die is besteed aan het uitvoeren van de toepassingscode, de procestijd die nodig is om de code van het besturingssysteem uit te voeren en de totale tijd die is besteed aan het uitvoeren van zowel de toepassings- als de code van het besturingssysteem.

CurrentDirectory

Hiermee haalt u het volledig gekwalificeerde pad van de huidige werkmap op of stelt u deze in.

CurrentManagedThreadId

Hiermee haalt u een unieke id op voor de huidige beheerde thread.

ExitCode

Hiermee haalt u de afsluitcode van het proces op of stelt u deze in.

HasShutdownStarted

Hiermee wordt een waarde opgehaald die aangeeft of het huidige toepassingsdomein wordt verwijderd of dat de COMMON Language Runtime (CLR) wordt afgesloten.

Is64BitOperatingSystem

Hiermee wordt een waarde opgehaald die aangeeft of het huidige besturingssysteem een 64-bits besturingssysteem is.

Is64BitProcess

Hiermee wordt een waarde opgehaald die aangeeft of het huidige proces een 64-bits proces is.

IsPrivilegedProcess

Hiermee wordt een waarde opgehaald die aangeeft of het huidige proces is gemachtigd om beveiligingsgerelateerde functies uit te voeren.

MachineName

Hiermee haalt u de NetBIOS-naam van deze lokale computer op.

NewLine

Hiermee haalt u de nieuwelijntekenreeks op die is gedefinieerd voor deze omgeving.

OSVersion

Hiermee haalt u de huidige platform-id en versienummer op.

ProcessId

Hiermee haalt u de unieke id voor het huidige proces op.

ProcessorCount

Hiermee haalt u het aantal processors op dat beschikbaar is voor het huidige proces.

ProcessPath

Retourneert het pad van het uitvoerbare bestand dat het momenteel uitgevoerde proces heeft gestart. Retourneert null wanneer het pad niet beschikbaar is.

StackTrace

Hiermee haalt u de huidige stacktraceringsgegevens op.

SystemDirectory

Hiermee haalt u het volledig gekwalificeerde pad van de systeemmap op.

SystemPageSize

Hiermee haalt u het aantal bytes op de geheugenpagina van het besturingssysteem op.

TickCount

Hiermee wordt het aantal milliseconden opgehaald dat is verstreken sinds het systeem is gestart.

TickCount64

Hiermee wordt het aantal milliseconden opgehaald dat is verstreken sinds het systeem is gestart.

UserDomainName

Hiermee haalt u de netwerkdomeinnaam op die is gekoppeld aan de huidige gebruiker.

UserInteractive

Hiermee wordt een waarde opgehaald die aangeeft of het huidige proces wordt uitgevoerd in de interactieve modus van de gebruiker.

UserName

Hiermee haalt u de gebruikersnaam op van de persoon die is gekoppeld aan de huidige thread.

Version

Hiermee haalt u een versie op die bestaat uit de primaire, secundaire, build- en revisienummers van de algemene taalruntime.

WorkingSet

Hiermee haalt u de hoeveelheid fysiek geheugen op die is toegewezen aan de procescontext.

Methoden

Name Description
Exit(Int32)

Hiermee wordt dit proces beëindigd en wordt een afsluitcode geretourneerd naar het besturingssysteem.

ExpandEnvironmentVariables(String)

Vervangt de naam van elke omgevingsvariabele die is ingesloten in de opgegeven tekenreeks door het tekenreeksequivalent van de waarde van de variabele en retourneert vervolgens de resulterende tekenreeks.

FailFast(String, Exception)

Hiermee wordt het proces onmiddellijk beëindigd voordat een foutbericht wordt gerapporteerd. Voor Windows wordt het foutbericht naar het gebeurtenislogboek van de Windows toepassing geschreven en wordt het bericht en de uitzonderingsinformatie opgenomen in foutrapportage voor Microsoft. Voor Unix-achtige systemen wordt het bericht naast de stacktracering naar de standaardfoutstroom geschreven.

FailFast(String)

Hiermee wordt het proces onmiddellijk beëindigd voordat een foutbericht wordt gerapporteerd. Voor Windows wordt het foutbericht naar het gebeurtenislogboek van de Windows toepassing geschreven en wordt het bericht opgenomen in foutrapportage voor Microsoft. Voor Unix-achtige systemen wordt het bericht, naast de stacktracering, naar de standaardfoutstroom geschreven.

GetCommandLineArgs()

Retourneert een tekenreeksmatrix met de opdrachtregelargumenten voor het huidige proces.

GetEnvironmentVariable(String, EnvironmentVariableTarget)

Haalt de waarde van een omgevingsvariabele op uit het huidige proces of uit de registersleutel van het Windows besturingssysteem voor de huidige gebruiker of lokale computer.

GetEnvironmentVariable(String)

Haalt de waarde van een omgevingsvariabele op uit het huidige proces.

GetEnvironmentVariables()

Haalt alle namen van omgevingsvariabelen en hun waarden op uit het huidige proces.

GetEnvironmentVariables(EnvironmentVariableTarget)

Haalt alle namen van omgevingsvariabelen en de bijbehorende waarden op uit het huidige proces, of uit de registersleutel van het Windows besturingssysteem voor de huidige gebruiker of lokale computer.

GetFolderPath(Environment+SpecialFolder, Environment+SpecialFolderOption)

Hiermee haalt u het pad naar de opgegeven speciale systeemmap op met behulp van een opgegeven optie voor toegang tot speciale mappen.

GetFolderPath(Environment+SpecialFolder)

Hiermee wordt het pad naar de opgegeven speciale map van het systeem opgehaald.

GetLogicalDrives()

Haalt de namen van de logische stations op deze computer op.

SetEnvironmentVariable(String, String, EnvironmentVariableTarget)

Hiermee maakt, wijzigt of verwijdert u een omgevingsvariabele die is opgeslagen in het huidige proces of in de Windows registersleutel van het besturingssysteem die is gereserveerd voor de huidige gebruiker of lokale computer.

SetEnvironmentVariable(String, String)

Hiermee maakt, wijzigt of verwijdert u een omgevingsvariabele die is opgeslagen in het huidige proces.

Van toepassing op