Environment Osztály
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Információkat és eszközöket biztosít a jelenlegi környezetről és platformról. Ez az osztály nem örökölhető.
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
- Öröklődés
-
Environment
- Attribútumok
Példák
Az alábbi példa az aktuális környezettel kapcsolatos információk listáját jeleníti meg.
// 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:\
'
Megjegyzések
Az osztály segítségével lekérheti a Environment parancssori argumentumokat, a kilépési kódot, a környezeti változó beállításait, a hívásverem tartalmát, a legutóbbi rendszerindítás óta eltelt időt és a közös nyelvi futtatókörnyezet verzióját.
Tulajdonságok
| Name | Description |
|---|---|
| CommandLine |
Lekéri a folyamat parancssorát. |
| CpuUsage |
Szerezze be a processzorhasználatot, beleértve az alkalmazáskód futtatásával töltött folyamatidőt, az operációs rendszer kódjának futtatásával töltött folyamatidőt, valamint az alkalmazás és az operációs rendszer kódjának futtatásával töltött teljes időt. |
| CurrentDirectory |
Lekéri vagy beállítja az aktuális munkakönyvtár teljes elérési útját. |
| CurrentManagedThreadId |
Az aktuális felügyelt szál egyedi azonosítójának lekérdezése. |
| ExitCode |
Lekéri vagy beállítja a folyamat kilépési kódját. |
| HasShutdownStarted |
Olyan értéket kap, amely jelzi, hogy az aktuális alkalmazástartomány kiürítve van-e, vagy a közös nyelvi futtatókörnyezet (CLR) leáll. |
| Is64BitOperatingSystem |
Olyan értéket kap, amely jelzi, hogy az aktuális operációs rendszer 64 bites operációs rendszer-e. |
| Is64BitProcess |
Olyan értéket kap, amely jelzi, hogy az aktuális folyamat 64 bites folyamat-e. |
| IsPrivilegedProcess |
Olyan értéket kap, amely jelzi, hogy az aktuális folyamat jogosult-e biztonsági szempontból releváns függvények végrehajtására. |
| MachineName |
Lekéri a helyi számítógép NetBIOS-nevét. |
| NewLine |
Lekéri a környezethez definiált újvonalas sztringet. |
| OSVersion |
Lekéri az aktuális platformazonosítót és verziószámot. |
| ProcessId |
Lekéri az aktuális folyamat egyedi azonosítót. |
| ProcessorCount |
Lekéri az aktuális folyamat számára elérhető processzorok számát. |
| ProcessPath |
Annak a végrehajthatónak az elérési útját adja vissza, amely elindította az aktuális végrehajtási folyamatot. Akkor adja |
| StackTrace |
Lekéri az aktuális verem nyomkövetési adatait. |
| SystemDirectory |
Lekéri a rendszerkönyvtár teljes elérési útját. |
| SystemPageSize |
Lekéri az operációs rendszer memóriaoldalán található bájtok számát. |
| TickCount |
A rendszer elindítása óta eltelt ezredmásodpercek számát adja meg. |
| TickCount64 |
A rendszer elindítása óta eltelt ezredmásodpercek számát adja meg. |
| UserDomainName |
Lekéri az aktuális felhasználóhoz társított hálózati tartománynevet. |
| UserInteractive |
Beolvas egy értéket, amely jelzi, hogy az aktuális folyamat felhasználói interaktív módban fut-e. |
| UserName |
Lekéri annak a személynek a felhasználónevét, aki az aktuális szálhoz van társítva. |
| Version |
Lekéri a közös nyelvi futtatókörnyezet fő-, alverzió-, build- és változatszámait tartalmazó verziót. |
| WorkingSet |
Lekéri a folyamatkörnyezethez hozzárendelt fizikai memória mennyiségét. |
Metódusok
| Name | Description |
|---|---|
| Exit(Int32) |
Leállítja ezt a folyamatot, és egy kilépési kódot ad vissza az operációs rendszernek. |
| ExpandEnvironmentVariables(String) |
A megadott sztringbe beágyazott környezeti változók nevét lecseréli a változó értékének megfelelő karakterláncra, majd visszaadja az eredményül kapott sztringet. |
| FailFast(String, Exception) |
A hibaüzenet jelentése előtt azonnal leállítja a folyamatot. A Windows esetén a hibaüzenet a Windows alkalmazás eseménynaplójába kerül, és az üzenet és a kivétel adatai szerepelnek a Microsoft való hibajelentésben. Unix-szerű rendszerek esetén a veremkövetés mellett az üzenet a standard hibastreambe lesz írva. |
| FailFast(String) |
A hibaüzenet jelentése előtt azonnal leállítja a folyamatot. A Windows a hibaüzenetet az alkalmazás Windows eseménynaplójába írja, és az üzenet szerepel a Microsoft való hibajelentésben. Unix-szerű rendszerek esetén az üzenet a veremkövetés mellett a standard hibastreambe van írva. |
| GetCommandLineArgs() |
Egy sztringtömböt ad vissza, amely az aktuális folyamat parancssori argumentumait tartalmazza. |
| GetEnvironmentVariable(String, EnvironmentVariableTarget) |
Lekéri egy környezeti változó értékét az aktuális folyamatból vagy az aktuális felhasználó vagy helyi gép Windows operációsrendszer-beállításkulcsából. |
| GetEnvironmentVariable(String) |
Lekéri egy környezeti változó értékét az aktuális folyamatból. |
| GetEnvironmentVariables() |
Lekéri az összes környezeti változó nevét és értékeit az aktuális folyamatból. |
| GetEnvironmentVariables(EnvironmentVariableTarget) |
Lekéri az összes környezeti változó nevét és értékeit az aktuális folyamatból, vagy az aktuális felhasználó vagy helyi gép Windows operációsrendszer-beállításkulcsából. |
| GetFolderPath(Environment+SpecialFolder, Environment+SpecialFolderOption) |
Lekéri a megadott speciális rendszermappa elérési útját a speciális mappák eléréséhez megadott beállítással. |
| GetFolderPath(Environment+SpecialFolder) |
Lekéri a megadott rendszer speciális mappájának elérési útját. |
| GetLogicalDrives() |
Lekéri a számítógép logikai meghajtóinak nevét. |
| SetEnvironmentVariable(String, String, EnvironmentVariableTarget) |
Létrehoz, módosít vagy töröl egy környezeti változót, amely az aktuális folyamatban vagy a Windows az aktuális felhasználó vagy helyi gép számára fenntartott operációsrendszer-beállításkulcsban van tárolva. |
| SetEnvironmentVariable(String, String) |
Létrehoz, módosít vagy töröl egy környezeti változót, amely az aktuális folyamatban van tárolva. |