在C#中,Environment类感觉还是蛮重要的,很多系统信息都可以通过Environment类来取得。先来个列表吧。
属性
Environment类的属性如下:
名称 | 说明 |
---|---|
CommandLine | 获取该进程的命令行。 |
CurrentDirectory | 获取或设置当前工作目录的完全限定路径。 |
CurrentManagedThreadId | 获取当前托管线程的唯一标识符。 |
ExitCode | 获取或设置进程的退出代码。 |
HasShutdownStarted | 取得是否开始关闭。 |
Is64BitOperatingSystem | 确定当前操作系统是否为 64 位操作系统。 |
Is64BitProcess | 确定当前进程是否为 64 位进程。 |
MachineName | 获取此本地计算机的 NetBIOS 名称。 |
NewLine | 获取为此环境定义的换行字符串。 |
OSVersion | 获取包含当前平台标识符和版本号的 OperatingSystem 对象。 |
ProcessorCount | 获取当前计算机上的处理器数。 |
StackTrace | 获取当前的堆栈跟踪信息。 |
SystemDirectory | 取得系统路径。 |
SystemPageSize | 获取操作系统的内存页的字节数。 |
TickCount | Gets the number of milliseconds elapsed since the system started. |
UserDomainName | Gets the network domain name associated with the current user. |
UserInteractive | 获取一个值,用以指示当前进程是否在用户交互模式中运行。 |
UserName | 获取当前已登录到 Windows 操作系统的人员的用户名。 |
Version | 获取一个 Version 对象,该对象描述公共语言运行时的主版本、次版本、内部版本和修订号。 |
WorkingSet | 获取映射到进程上下文的物理内存量。 |
方法
Environment类的方法如下:
名称 | 说明 |
---|---|
Exit(Int32) | Terminates this process and returns an exit code to the operating system. |
ExpandEnvironmentVariables(String) | Replaces the name of each environment variable embedded in the specified string with the string equivalent of the value of the variable, then returns the resulting string. |
FailFast(String) | 向 Windows 的应用程序事件日志写入消息后立即终止进程,然后在发往 Microsoft 的错误报告中加入该消息。 |
FailFast(String, Exception) | 向 Windows 的应用程序事件日志写入消息后立即终止进程,然后在发往 Microsoft 的错误报告中加入该消息和异常信息。 |
GetCommandLineArgs() | Returns a string array containing the command-line arguments for the current process. |
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 操作系统注册表项中存储的环境变量。 |
TickCount | Gets the number of milliseconds elapsed since the system started. |
UserDomainName | Gets the network domain name associated with the current user. |
UserInteractive | 获取一个值,用以指示当前进程是否在用户交互模式中运行。 |
UserName | 获取当前已登录到 Windows 操作系统的人员的用户名。 |
Version | 获取一个 Version 对象,该对象描述公共语言运行时的主版本、次版本、内部版本和修订号。 |
WorkingSet | 获取映射到进程上下文的物理内存量。 |
测试代码
我们可以用下面的代码来测试
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test
{
class Program
{
static void Main(string[] args)
{
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));
}
}
}
参考地址:
Environment 类.aspx)