C#学习之System.Environment类

来源:互联网 发布:黑马程序员官网登录 编辑:程序博客网 时间:2024/06/07 00:08

一,Environment类允许我们通过不同的静态成员获得大量有关运行.NET应用程序的操作系统的细节。

1,Environment.GetCommandLineArgs()-访问命令行参数

static int Main(string[] args)//输入参数string数组可以去掉{    string[] theArgs = Environment.GetCommandLineArgs();//作用相当于输入参数的string数组    foreach(string arg in theArgs)        Console.WriteLine("Arg:{0}",arg);    Console.ReadLine();    return -1;}

2,GetLogicalDrives(),OSVersion,ProcessorCount,Version

static int Main(string[] args){    ShowEnvironmentDetails();    Console.ReadLine();    return -1;}static void ShowEnvironmentDetails(){    //输出本机的驱动器以及其他一些有用的细节信息    foreach(string drive in Environment.GetLogicalDrives())        Console.WriteLine("Drive:{0}",drive);    Console.WriteLine("OS:{0}",Environment.OSVersion);    Console.WriteLine("Number of processors:{0}",Environment.ProcessorCount);    Console.WriteLine(".NET Version:{0}",Environment.Version);}/*input:Drive:C:\Drive:D:\Drive:E:\OS:Microsoft Windows NT 6.1.7601 Service Pack 1Number of processors:4.NET Version:4.0.30319.42000*/

3,System.Environment的其他部分属性
ExitCode,获取或设置应用程序中任何地方的退出代码
Is64BitOperatingSystem,返回布尔值,代表主机是否运行64位操作系统
MachineName,获取当前机器的名字
NewLine,获得当前环境的换行符
SystemDirectory,返回通向系统目录的完整路径
UserName,返回启动这个应用程序的用户的名称

0 0
原创粉丝点击