共用方式為


Environment 類別

定義

提供有關目前環境和平台的資訊,以及操作的方法。 此類別無法獲得繼承。

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
繼承
Environment
屬性

範例

下列範例會顯示目前環境的相關資訊清單。

// 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:\
'

備註

Environment使用 類別來擷取命令列引數、結束代碼、環境變數設定、呼叫堆疊的內容、上次系統開機後的時間,以及 Common Language Runtime 的版本等資訊。

屬性

CommandLine

取得這個處理程序的命令列。

CpuUsage

提供有關目前環境和平台的資訊,以及操作的方法。 此類別無法獲得繼承。

CurrentDirectory

取得或設定目前工作目錄的完整路徑。

CurrentManagedThreadId

取得目前 Managed 執行緒的唯一識別項。

ExitCode

取得或設定處理序的結束代碼。

HasShutdownStarted

取得值,這個值表示正在卸載目前的應用程式定義域,或是正在關閉通用語言執行平台 (CLR)。

Is64BitOperatingSystem

取得值,指出目前的作業系統是否為 64 位元作業系統。

Is64BitProcess

取得值,指出目前的處理序是否為 64 位元處理序。

IsPrivilegedProcess

取得值,這個值表示目前進程是否獲授權執行安全性相關函式。

MachineName

取得這個本機電腦的 NetBIOS 名稱。

NewLine

取得為這個環境定義的新行字串 (Newline String)。

OSVersion

取得目前的平台識別碼與版本號碼。

ProcessId

取得目前處理序的唯一識別碼。

ProcessorCount

取得目前進程可用的處理器數目。

ProcessPath

傳回啟動目前執行中進程的可執行檔路徑。 當路徑無法使用時傳 null 回 。

StackTrace

取得目前的堆疊追蹤資訊。

SystemDirectory

取得系統目錄的完整路徑。

SystemPageSize

取得作業系統記憶體分頁中的位元組數目。

TickCount

取得系統啟動後經過的毫秒數。

TickCount64

取得系統啟動後經過的毫秒數。

UserDomainName

取得與目前使用者相關聯的網路網域名稱。

UserInteractive

取得數值,指示目前的處理程序是否正執行於使用者互動模式中。

UserName

取得與目前執行緒建立關聯的人員使用者名稱。

Version

取得包含主要版本、次要版本、組建與 Common Language Runtime 修訂編號的版本號碼。

WorkingSet

取得對應至處理序內容的實體記憶體數量。

方法

Exit(Int32)

結束這個處理序,並傳回結束代碼給作業系統。

ExpandEnvironmentVariables(String)

用變數值的等值字串來取代內嵌於指定字串內的每個環境變數名稱,然後傳回結果字串。

FailFast(String)

在報告錯誤訊息之前,立即終止進程。 針對 Windows,錯誤訊息會寫入 Windows 應用程式事件記錄檔,而且訊息會包含在向 Microsoft 回報的錯誤中。 針對類似 Unix 的系統,訊息會連同堆疊追蹤一起寫入標準錯誤資料流程。

FailFast(String, Exception)

在報告錯誤訊息之前,立即終止進程。 針對 Windows,錯誤訊息會寫入 Windows 應用程式事件記錄檔,並將訊息和例外狀況資訊包含在向 Microsoft 回報的錯誤中。 針對類似 Unix 的系統,堆疊追蹤旁的訊息會寫入標準錯誤資料流程。

GetCommandLineArgs()

傳回字串陣列,包含目前處理序的命令列引數。

GetEnvironmentVariable(String)

從目前的處理程序中擷取環境變數的值。

GetEnvironmentVariable(String, EnvironmentVariableTarget)

從目前的處理程序或是目前使用者或本機電腦的 Windows 作業系統登錄機碼中,擷取環境變數的值。

GetEnvironmentVariables()

從目前的處理序中擷取所有環境變數名稱及它們的值。

GetEnvironmentVariables(EnvironmentVariableTarget)

從目前的處理程序或是目前使用者或本機電腦的 Windows 作業系統登錄機碼中,擷取所有環境變數名稱和它們的值。

GetFolderPath(Environment+SpecialFolder)

取得指定之系統特殊資料夾的路徑。

GetFolderPath(Environment+SpecialFolder, Environment+SpecialFolderOption)

使用指定選項存取特殊資料夾,取得指定之系統特殊資料夾的路徑。

GetLogicalDrives()

傳回包含目前電腦上邏輯磁碟名稱的字串陣列。

SetEnvironmentVariable(String, String)

建立、修改或刪除儲存在目前處理序中的環境變數。

SetEnvironmentVariable(String, String, EnvironmentVariableTarget)

建立、修改或刪除儲存在目前的處理序中,或是儲存在保留給目前使用者或本機電腦的 Windows 作業系統登錄機碼中的環境變數。

適用於