System.Environment 类

方法描述

提供有关当前环境和平台的信息以及操作它们的方法。 此类不能被继承。

语法定义(C# System.Environment 类 的用法)

[ComVisibleAttribute(true)]
public static class Environment

构造函数

构造函数名称 构造函数描述

成员/方法

方法名称 方法描述
Exit 终止此进程并为基础操作系统提供指定的退出代码。
ExpandEnvironmentVariables 将嵌入到指定字符串中的每个环境变量的名称替换为该变量的值的等效字符串,然后返回结果字符串。
FailFast(String) 向 Windows 的应用程序事件日志写入消息后立即终止进程,然后在发往 Microsoft 的错误报告中加入该消息。
FailFast(String, Exception) 向 Windows 的应用程序事件日志写入消息后立即终止进程,然后在发往 Microsoft 的错误报告中加入该消息和异常信息。
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 操作系统注册表项中存储的环境变量。

提示和注释

使用 Environment 类可检索信息,如命令行参数、退出代码、环境变量设置、调用堆栈的内容、自上次系统启动以来的时间,以及公共语言运行时的版本。

System.Environment 类例子

下面的示例演示如何显示有关当前环境的信息列表。

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

*/

继承层次结构

System.Object

System.Environment

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。

相关资源

System 命名空间
MSDN