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

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Collections;namespace命令行参数{classProgram{staticintMain(string[]args){//通过Environment.GetCommandLineArgs()获取命令行参数//第一个索引为应用程序本身名称//数组中其他元素包含单独的命令行参数string[]theArg=Environment.GetCommandLineArgs();foreach(stringargintheArg){Console.WriteLine("Arg:{0}",arg);}ShowEnvironmentDetails();Console.WriteLine("退出后");//这一句不会执行return0;}staticvoidShowEnvironmentDetails(){//从当前进程检索所有环境变量名及其值IDictionaryenvironmentVariables=Environment.GetEnvironmentVariables();foreach(DictionaryEntrydeinenvironmentVariables){Console.WriteLine("{0}----{1}",de.Key,de.Value);}Console.WriteLine("--------------------------------------");//返回包含当前计算机中的逻辑驱动器名称的字符串数组Console.WriteLine("当前电脑有{0}个逻辑驱动器",Environment.GetLogicalDrives().Length);foreach(stringdriveinEnvironment.GetLogicalDrives()){Console.WriteLine("drive:{0}",drive);}//判断当前操作系统是否为64位操作系统Console.WriteLine("当前系统为64位系统:{0}",Environment.Is64BitOperatingSystem);//获取本地计算机的NETBIOS名称Console.WriteLine("计算机的名称为:{0}",Environment.MachineName);//获取系统目录的完全限定路径Console.WriteLine("系统目录的完全限定路径:{0}",Environment.SystemDirectory);//获取当前计算机上的处理器数Console.WriteLine("当前计算机上的处理器数为:{0}",Environment.ProcessorCount);//获取操作系统页面文件的内存量Console.WriteLine("当前操作系统页面文件的内存量为:{0}",Environment.SystemPageSize);//获取系统启动后经过的毫秒数自系统启动以来到现在所经过的时间Console.WriteLine("系统启动后经过的毫秒数:{0}",Environment.TickCount);//获取当前已登录到windows操作系统的人员的用户名Console.WriteLine("当前登录操作系统的用户名为:{0}",Environment.UserName);//获取包含当前平台标识符和版本号的operatingSystem对象Console.WriteLine("当前操作系统为:{0}",Environment.OSVersion);//获取一个Version对象,该对象描述公共语言运行时的主版本、此版本、内部版本和修订号Console.WriteLine(".NETVersion:{0}",Environment.Version);Console.ReadLine();//程序结束终止此进程并为基础操作系统提供指定的退出代码Environment.Exit(-1);Console.WriteLine("退出");//这一句也不会被执行}}}



要想了解更多System.Environment类的成员,请查阅msdn。