Environment Třída

Definice

Poskytuje informace a prostředky pro manipulaci s aktuálním prostředím a platformou. Tato třída se nemůže dědit.

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
Dědičnost
Environment
Atributy

Příklady

Následující příklad zobrazí seznam informací o aktuálním prostředí.

// Sample for Environment class summary
using namespace System;
using namespace System::Collections;
int 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 );
   array<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 );
   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 );
   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();
   IEnumerator^ myEnum = environmentVariables->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      DictionaryEntry^ de = safe_cast<DictionaryEntry^>(myEnum->Current);
      Console::WriteLine( " {0} = {1}", de->Key, de->Value );
   }

   Console::WriteLine( "GetFolderPath: {0}", Environment::GetFolderPath( Environment::SpecialFolder::System ) );
   array<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 S"!---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
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:\
'

Poznámky

Environment Pomocí třídy načtěte informace, jako jsou argumenty příkazového řádku, ukončovací kód, nastavení proměnných prostředí, obsah zásobníku volání, čas od posledního spuštění systému a verze modulu CLR (Common Language Runtime).

Vlastnosti

CommandLine

Získá příkazový řádek pro tento proces.

CurrentDirectory

Získá nebo nastaví plně kvalifikovanou cestu aktuálního pracovního adresáře.

CurrentManagedThreadId

Získá jedinečný identifikátor pro aktuální spravované vlákno.

ExitCode

Získá nebo nastaví ukončovací kód procesu.

HasShutdownStarted

Získá hodnotu, která označuje, zda aktuální aplikační doména je uvolněna nebo ClR clR (Common Language Runtime) se ukončuje.

Is64BitOperatingSystem

Získá hodnotu, která označuje, zda aktuální operační systém je 64bitový operační systém.

Is64BitProcess

Získá hodnotu, která označuje, zda aktuální proces je 64bitový proces.

IsPrivilegedProcess

Získá hodnotu, která označuje, zda aktuální proces je oprávněn provádět funkce související se zabezpečením.

MachineName

Získá název NetBIOS tohoto místního počítače.

NewLine

Získá nový řetězec definovaný pro toto prostředí.

OSVersion

Získá aktuální identifikátor platformy a číslo verze.

ProcessId

Získá jedinečný identifikátor aktuálního procesu.

ProcessorCount

Získá počet procesorů dostupných pro aktuální proces.

ProcessPath

Vrátí cestu spustitelného souboru, který spustil aktuálně spuštěný proces. Vrátí null , když cesta není k dispozici.

StackTrace

Získá aktuální informace o trasování zásobníku.

SystemDirectory

Získá plně kvalifikovanou cestu systémového adresáře.

SystemPageSize

Získá počet bajtů na stránce paměti operačního systému.

TickCount

Získá počet milisekund od spuštění systému.

TickCount64

Získá počet milisekund od spuštění systému.

UserDomainName

Získá název domény sítě přidružené k aktuálnímu uživateli.

UserInteractive

Získá hodnotu označující, zda aktuální proces je spuštěn v interaktivním režimu uživatele.

UserName

Získá uživatelské jméno osoby, která je přidružena k aktuálnímu vláknu.

Version

Získá verzi, která se skládá z hlavních, podverze, sestavení a revizí common language runtime.

WorkingSet

Získá množství fyzické paměti mapované na kontext procesu.

Metody

Exit(Int32)

Ukončí tento proces a vrátí ukončovací kód do operačního systému.

ExpandEnvironmentVariables(String)

Nahradí název každé proměnné prostředí vložené do zadaného řetězce řetězcovým ekvivalentem hodnoty proměnné a pak vrátí výsledný řetězec.

FailFast(String)

Okamžitě ukončí proces před oznámením chybové zprávy. V systému Windows se chybová zpráva zapíše do protokolu událostí aplikace systému Windows a zpráva je součástí zasílání zpráv o chybách společnosti Microsoft. U systémů typu Unix se zpráva spolu s trasování zásobníku zapisuje do standardního datového proudu chyb.

FailFast(String, Exception)

Okamžitě ukončí proces před oznámením chybové zprávy. V systému Windows je chybová zpráva zapsána do protokolu událostí aplikace systému Windows a zpráva a informace o výjimce jsou součástí zasílání zpráv o chybách společnosti Microsoft. U systémů typu Unix se zpráva spolu s trasování zásobníku zapisuje do standardního datového proudu chyb.

GetCommandLineArgs()

Vrátí pole řetězců obsahující argumenty příkazového řádku pro aktuální proces.

GetEnvironmentVariable(String)

Načte hodnotu proměnné prostředí z aktuálního procesu.

GetEnvironmentVariable(String, EnvironmentVariableTarget)

Načte hodnotu proměnné prostředí z aktuálního procesu nebo z klíče registru operačního systému Windows pro aktuálního uživatele nebo místní počítač.

GetEnvironmentVariables()

Načte všechny názvy proměnných prostředí a jejich hodnoty z aktuálního procesu.

GetEnvironmentVariables(EnvironmentVariableTarget)

Načte všechny názvy proměnných prostředí a jejich hodnoty z aktuálního procesu nebo z klíče registru operačního systému Windows pro aktuálního uživatele nebo místní počítač.

GetFolderPath(Environment+SpecialFolder)

Získá cestu k zadané zvláštní složce systému.

GetFolderPath(Environment+SpecialFolder, Environment+SpecialFolderOption)

Získá cestu k zadané zvláštní složce systému pomocí zadané možnosti pro přístup ke speciálním složkám.

GetLogicalDrives()

Vrátí pole řetězce obsahující názvy logických jednotek v aktuálním počítači.

SetEnvironmentVariable(String, String)

Vytvoří, upraví nebo odstraní proměnnou prostředí uloženou v aktuálním procesu.

SetEnvironmentVariable(String, String, EnvironmentVariableTarget)

Vytvoří, upraví nebo odstraní proměnnou prostředí uloženou v aktuálním procesu nebo v klíči registru operačního systému Windows vyhrazeném pro aktuálního uživatele nebo místní počítač.

Platí pro