提供有关当前环境和平台的信息以及操作它们的方法。无法继承此类。
方法
名称 | 说明 | |
---|---|---|
Exit | 终止此进程并为基础操作系统提供指定的退出代码。 | |
ExpandEnvironmentVariables | 将嵌入到指定字符串中的每个环境变量的名称替换为该变量的值的等效字符串,然后返回结果字符串。 | |
FailFast | 终止进程但不执行任何活动 try-finally 块或终结器。 | |
GetCommandLineArgs | 返回包含当前进程的命令行参数的字符串数组。 | |
GetEnvironmentVariable | 已重载。 检索环境变量的值。 | |
GetEnvironmentVariables | 已重载。 检索所有环境变量名及其值。 | |
GetFolderPath | 获取指向由指定枚举标识的系统特殊文件夹的路径。 | |
GetLogicalDrives | 返回包含当前计算机中的逻辑驱动器名称的字符串数组。 | |
SetEnvironmentVariable | 已重载。 创建、修改或删除环境变量。 |
属性
名称 | 说明 | |
---|---|---|
CommandLine | 获取该进程的命令行。 | |
CurrentDirectory | 获取或设置当前工作目录的完全限定路径。 | |
ExitCode | 获取或设置进程的退出代码。 | |
HasShutdownStarted | 获取一个值,该值指示是否公共语言运行库正在关闭或者当前的应用程序域正在卸载。 | |
MachineName | 获取此本地计算机的 NetBIOS 名称。 | |
NewLine | 获取为此环境定义的换行字符串。 | |
OSVersion | 获取包含当前平台标识符和版本号的 OperatingSystem 对象。 | |
ProcessorCount | 获取当前计算机上的处理器数。 | |
StackTrace | 获取当前的堆栈跟踪信息。 | |
SystemDirectory | 获取系统目录的完全限定路径。 | |
TickCount | 获取系统启动后经过的毫秒数。 | |
UserDomainName | 获取与当前用户关联的网络域名。 | |
UserInteractive | 获取一个值,用以指示当前进程是否在用户交互模式中运行。 | |
UserName | 获取当前已登录到 Windows 操作系统的人员的用户名。 | |
Version | 获取一个 Version 对象,该对象描述公共语言运行库的主版本、次版本、内部版本和修订号。 | |
WorkingSet | 获取映射到进程上下文的物理内存量。 |
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:\
*/
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:\
*/