C#获取用户系统信息的程序

介绍

在这个程序当中,主要通过下面两个类获取用户系统的信息:

  1. System.Environment
  2. System.Managememnt

应用程序包括三个tab菜单,分别是:

  1. System
  2. CPU
  3. Local drives

程序外观如下图:

 

在 System 菜单中,用 System.Environment 类来获取系统信息 :

 

private string SystemInformation()
{
    StringBuilder StringBuilder1 = new StringBuilder(string.Empty);
    try
    {
        StringBuilder1.AppendFormat("Operation System:  {0}\n", Environment.OSVersion);
        if (Environment.Is64BitOperatingSystem)
            StringBuilder1.AppendFormat("\t\t  64 Bit Operating System\n");
        else
            StringBuilder1.AppendFormat("\t\t  32 Bit Operating System\n");
        StringBuilder1.AppendFormat("SystemDirectory:  {0}\n", Environment.SystemDirectory);
        StringBuilder1.AppendFormat("ProcessorCount:  {0}\n", Environment.ProcessorCount);
        StringBuilder1.AppendFormat("UserDomainName:  {0}\n", Environment.UserDomainName);
        StringBuilder1.AppendFormat("UserName: {0}\n", Environment.UserName);
        //Drives
        StringBuilder1.AppendFormat("LogicalDrives:\n");
        foreach (System.IO.DriveInfo DriveInfo1 in System.IO.DriveInfo.GetDrives())
        {
            try
            {
                StringBuilder1.AppendFormat("\t Drive: {0}\n\t\t VolumeLabel: " + 
                  "{1}\n\t\t DriveType: {2}\n\t\t DriveFormat: {3}\n\t\t " + 
                  "TotalSize: {4}\n\t\t AvailableFreeSpace: {5}\n",
                  DriveInfo1.Name, DriveInfo1.VolumeLabel, DriveInfo1.DriveType, 
                  DriveInfo1.DriveFormat, DriveInfo1.TotalSize, DriveInfo1.AvailableFreeSpace);
            }
            catch
            {
            }
        }
        StringBuilder1.AppendFormat("SystemPageSize:  {0}\n", Environment.SystemPageSize);
        StringBuilder1.AppendFormat("Version:  {0}", Environment.Version);
    }
    catch
    {
    }
    return StringBuilder1.ToString();
}

在 CPU 和 Local drive 选项卡, 类 System.Management 用来收集信息,但是在使用该类之前需要加入引用. 加入System.Management引用的步骤如下:

  1. 打开项目解决方案.
  2. 右键单击引用,选择添加引用.
  3. 单击.NET 选项卡.
  4. 选择System.Management.
  5. 单击确定按钮.

 现在可以用该类写下面的函数:

private string DeviceInformation(string stringIn)
{
    StringBuilder StringBuilder1 = new StringBuilder(string.Empty);
    ManagementClass ManagementClass1 = new ManagementClass(stringIn);
    //Create a ManagementObjectCollection to loop through
    ManagementObjectCollection ManagemenobjCol = ManagementClass1.GetInstances();
    //Get the properties in the class
    PropertyDataCollection properties = ManagementClass1.Properties;
    foreach (ManagementObject obj in ManagemenobjCol)
    {
        foreach (PropertyData property in properties)
        {
            try
            {
                StringBuilder1.AppendLine(property.Name + ":  " + 
                  obj.Properties[property.Name].Value.ToString());
            }
            catch
            {
                //Add codes to manage more informations
            }
        }
        StringBuilder1.AppendLine();
    }
    return StringBuilder1.ToString();
}


这个函数有一个参数,该参数的值可以是Win32_Processor 或Win32_LogicalDisk。用第一次参数来获取CPU的信息,用第二参数来获取硬盘驱动器的信息。最后再Window_Loaded函数中调用如下语句:

 

//System Information
textBox1.Text = SystemInformation();
//CPU Information
textBox2.Text = DeviceInformation("Win32_Processor");
//Local Drives Information
textBox3.Text = DeviceInformation("Win32_LogicalDisk");



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值