C#获取计算机硬件的参数信息

时间:2022-12-24 17:52:51

  2019年的时候用PowerBuilder写过一个计算机信息收集小程序,《计算机信息收集小程序》,当时是查注册表来实现的。

C#获取计算机硬件的参数信息

   还可以通过对windows的API调用来做,这个稍微有点麻烦。

  使用C#也可以通过三种方式来实现,分别是读取注册表、windows的API调用、读取设备数据库,其中读取设备数据库比较方便快捷。

  这个星期居家办公很不方便,打算就用C#来写这个小程序。

C#获取计算机硬件的参数信息

  完成了硬件参数、操作系统、网络信息、软件以及安全信息的读取,后面就可以存入数据库方便网络上的调取和查询。

  思路:

  1、System.Management命名空间来获取对设备参数的提取;

  2、建立对象并设置好属性字段,逐个赋予对象属性字段的值;

  3、将对象序列化;

  4、反序列化读取对象的字段属性值,放入字典中;

  5、根据属性名逐一读取硬件参数。

  下面是读取硬件参数的代码: 

    public class OSInformation
    {
        public string OSName { get; set; }
        public string OSType { get; set; }
        public string OSSerialNumber { get; set; }
        public string ComputerName { get; set; }
        public string LogonUserName { get; set; }
    }

    public class HInformaiton
    {
        public string MainboardName { get; set; }
        public string MemorySize { get; set; }
        public string HarddiskSize { get; set; }
        public string VideocardName { get; set; }
        public string SoundcardName { get; set; }
        public string NetworkcardName { get; set; }
        public string ManufacturerName { get; set; }
        public string ManufactureDate { get; set; }
    }

    public class SInformation
    {
        public string VRV { get; set; }
        public string QAX { get; set; }
        public string WPS { get; set; }
    }

    public class WInformation
    {
        public string IP { get; set; }
        public string Subnetmask { get; set; }
        public string Gateway { get; set; }
    }

    class HardwareInformation
    {
        public static string GetJsonStr()
        {
            string ReturnStrJson = "";
            var HInfor = new HInformaiton { MainboardName ="未知", MemorySize = "未知", HarddiskSize = "未知", VideocardName = "未知", SoundcardName = "未知", NetworkcardName = "未知",ManufacturerName= "未知", ManufactureDate= "未知" };

            //主板信息
            try {
                ManagementObjectSearcher myMainboard = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");
                foreach (ManagementObject board in myMainboard.Get())
                {
                    HInfor.MainboardName = board["Product"].ToString();
                    //break;
                }
            }
            catch (Exception ex) {
                HInfor.MainboardName = "未知";
            }

            //内存信息
            try {
                ManagementObjectSearcher myMemory = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMemory");
                //定义内存总大小变量
                double MemorySize = 0;
                foreach (ManagementObject obj in myMemory.Get())
                {
                    //将内存大小换算成G单位
                    MemorySize += Convert.ToDouble(obj["Capacity"]) / 1024 / 1024 / 1024;
                }
                HInfor.MemorySize = MemorySize.ToString()+"G";
            } catch (ManagementException e) {
                HInfor.MemorySize = "未知";
            }

            //硬盘信息
            try {
                //获取本机所有硬盘信息  
                ManagementObjectSearcher myHarddisk = new ManagementObjectSearcher("select * from Win32_DiskDrive");
                foreach (ManagementObject drive in myHarddisk.Get())
                {
                    //获取硬盘容量
                    var capacity = (Convert.ToDouble(drive["Size"]) / 1024 / 1024 / 1024).ToString("0.00") + "G";
                    //获取硬盘类型
                    var mediaType = drive["MediaType"];
                    HInfor.HarddiskSize = capacity.ToString() + "|" + mediaType.ToString();
                }
            }
            catch (Exception ex) {
                HInfor.HarddiskSize = "未知";
            }

            //显卡信息
            try {
                ManagementObjectSearcher myVideoObject = new ManagementObjectSearcher("select * from Win32_VideoController");
                foreach (ManagementObject obj in myVideoObject.Get())
                {
                    HInfor.VideocardName = obj["Name"].ToString();
                }
            } catch (ManagementException e) {
                //e.Message
                HInfor.VideocardName = "未知";
            }

            //声卡信息
            try {
                // 创建WMI搜索对象
                ManagementObjectSearcher mySoundcard = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_SoundDevice");
                // 遍历搜索结果
                foreach (ManagementObject mo in mySoundcard.Get())
                {
                    // 获取声卡名称
                    HInfor.SoundcardName = mo.GetPropertyValue("Name").ToString();
                }
            } catch (ManagementException e) {
                HInfor.SoundcardName = "未知";
            }

            //网卡信息(Mac地址)
            try {

                // 获取本地网络接口信息 
                NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface adapter in nics)
                {
                    // 如果是RJ45网卡 
                    if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                    {
                        string S1 = (string)adapter.Description;
                        if (S1.Contains("PCI")){
                            HInfor.NetworkcardName = S1;
                        }
                    }
                }
            }
            catch (ManagementException e) {
                HInfor.NetworkcardName = "未知";
            }

            //获取生产商和生产日期
            try
            {
                //获取生产商
                ManagementObjectSearcher ManufacturerInfo = new ManagementObjectSearcher("select * from Win32_ComputerSystem");
                foreach (ManagementObject obj in ManufacturerInfo.Get())
                {
                    HInfor.ManufacturerName = obj["Manufacturer"].ToString();
                }
                //获取生产日期
                ConnectionOptions options = new ConnectionOptions();
                ManagementScope scope = new ManagementScope("\\\\.\\root\\cimv2", options);
                ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_BIOS");
                ManagementObjectSearcher BoisInfo = new ManagementObjectSearcher(scope, query);
                foreach (ManagementObject mo in BoisInfo.Get())
                {
                    string StrManufactureDate = mo["ReleaseDate"].ToString().Substring(0, 8);
                    DateTime DT = DateTime.ParseExact(StrManufactureDate, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
                    HInfor.ManufactureDate = String.Format("{0:d}", DT);
                }
            }
            catch (Exception e) { 
            
            }

            ReturnStrJson = JsonSerializer.Serialize(HInfor);
            return ReturnStrJson;
        }
    }

  需要添加引用:

using Microsoft.Win32;
using System.Management;
using System.Text.Json;
using System.Net.NetworkInformation;

  调用比较简单:

            string JsonStr;
            //硬件信息
            JsonStr = HardwareInformation.GetJsonStr();
            var ObjHardware = JsonSerializer.Deserialize<Dictionary<string, object>>(JsonStr);
            ListBox listbox1 = tabControl1.TabPages[0].Controls["listbox1"] as ListBox;
            listbox1.Items.Add("主板:"+ ObjHardware["MainboardName"]);
            listbox1.Items.Add("内存:" + ObjHardware["MemorySize"]);
            listbox1.Items.Add("硬盘:" + ObjHardware["HarddiskSize"]);
            listbox1.Items.Add("显卡:" + ObjHardware["VideocardName"]);
            listbox1.Items.Add("声卡:" + ObjHardware["SoundcardName"]);
            listbox1.Items.Add("网卡:" + ObjHardware["NetworkcardName"]);
            listbox1.Items.Add("计算机生产商:" + ObjHardware["ManufacturerName"]);
            listbox1.Items.Add("计算机生产日期:" + ObjHardware["ManufactureDate"]);

  使用的是Visual Studio 2022 社区版,后面再进行优化。