当前位置:主页 > 软件编程 > .NET代码 >

C#使用System.Environment获取电脑的相关属性

时间:2020-12-10 09:07:58 | 栏目:.NET代码 | 点击:

使用System.Environment获取电脑的相关属性,入门案例,具体内容如下

static void Main(string[] args)
  {
   MyComputer myNotebook = new MyComputer();
   myNotebook.ComputerInfo();
   Console.ReadKey();
  }

  public class MyComputer
  {
   private string Domain;
   private string No;
   private string User;
   private string[] Drives;
   private string OS;
   private Version Version;
   private int ProcessorCount;

   public string Name
   {
    get
    {
     return this.No;
    }
   }
   public MyComputer()
   {
    this.User = System.Environment.UserName;
    this.No = System.Environment.MachineName;
    this.Domain = System.Environment.UserDomainName;
    this.Drives = System.Environment.GetLogicalDrives();
    this.OS = System.Environment.OSVersion.ToString();
    this.Version = System.Environment.Version;
    this.ProcessorCount = System.Environment.ProcessorCount;
   }
   public void ComputerInfo()
   {
    Console.WriteLine("当前电脑所在的域名:{0}", Domain);
    Console.WriteLine("当前电脑的用户是:{0}", User);
    Console.WriteLine("当前电脑的名字是:{0}", No);
    Console.WriteLine("当前电脑的操作系统版本:{0}", OS);
    Console.WriteLine("当前电脑的系统版本:{0}", Version);
    Console.WriteLine("当前电脑的处理器个数:{0}", ProcessorCount);
    Console.WriteLine("当前电脑的驱动器有:");
    foreach (var item in Drives)
    {
     Console.WriteLine("{0}驱动器下的目录有:", item);
     Console.WriteLine("=========================================================");
     string[] d = Directory.GetDirectories(item);
     foreach (var d1 in d)
     {
      Console.WriteLine(d1);
     }
    }
   }
  }

您可能感兴趣的文章:

相关文章