WMI获取硬件信息封装函数方法(联想台式机出厂编号 CPUID BIOS序列号 硬盘信息 显卡信息 MAC地址)

时间:2023-10-03 13:39:54 

今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都是可以提取出来的,就自己把那些公共部分提出出来,以后如果要获取某部分的硬件信息就不用写一个一个的函数,比如获取MAC地址就写一个获取MAC地址的函数,获取CPU 信息就写一个获取CPU信息的函数,太麻烦了

如下是函数代码:


private static string identifier(string wmiClass, string wmiProperty, string wmiMustBeTrue)
        {
            string result = "";
            System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
            System.Management.ManagementObjectCollection moc = mc.GetInstances();
            foreach (System.Management.ManagementObject mo in moc)
            {
                if (mo[wmiMustBeTrue].ToString() == "True")
                {
                    if (result == "")
                    {
                        try
                        {
                            result = mo[wmiProperty].ToString();
                            break;
                        }
                        catch
                        {
                        }
                    }

                }
            }
            return result;
        }


        private static string identifier(string wmiClass, string wmiProperty)
        {
            string result = "";
            System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
            System.Management.ManagementObjectCollection moc = mc.GetInstances();
            foreach (System.Management.ManagementObject mo in moc)
            {
                if (result == "")
                {
                    try
                    {
                        result = mo[wmiProperty].ToString();
                        break;
                    }
                    catch
                    {
                    }
                }

            }
            return result;
        }

获取CPUID


private static string cpuId()
        {    
            string retVal = identifier("Win32_Processor", "UniqueId");  //CPUID  
            retVal += identifier("Win32_Processor", "ProcessorId");
            retVal += identifier("Win32_Processor", "Name");  //处理器名称
            retVal += identifier("Win32_Processor", "Manufacturer");  //处理器制造商
            retVal +=identifier("Win32_Processor", "MaxClockSpeed");  //最大时钟频率
            return retVal;
        }

获取BIOS信息,其中BIOS序列号就是联想台式机的出厂编号,我看联想的保修页面里的自动获取主机编号应该也是调用这个"Win32_BIOS"的 "SerialNumber

报修页面网址:http://support1.lenovo.com.cn/lenovo/wsi/wsbx/lenovo/#minarepairInfo


//BIOS信息
        private static string biosId()
        {
            return identifier("Win32_BIOS", "Manufacturer")          //BIOS制造商名称
                    + identifier("Win32_BIOS", "SMBIOSBIOSVersion")  //
                    + identifier("Win32_BIOS", "IdentificationCode") //
                    + identifier("Win32_BIOS", "SerialNumber")       //BIOS序列号
                    + identifier("Win32_BIOS", "ReleaseDate")        //出厂日期
                    + identifier("Win32_BIOS", "Version");           //版本号
        }

获取硬盘信息:


private static string diskId()
        {
            return identifier("Win32_DiskDrive", "Model")           //模式
                    + identifier("Win32_DiskDrive", "Manufacturer") //制造商
                    + identifier("Win32_DiskDrive", "Signature")    //签名
                    + identifier("Win32_DiskDrive", "TotalHeads");  //扇区头
        }

获取显卡信息:


private static string videoId()
         {
            return identifier("Win32_VideoController", "DriverVersion")
                     + identifier("Win32_VideoController", "Name");
        }

获取网卡MAC地址信息:


private static string macId()
         {
             return identifier("Win32_NetworkAdapterConfiguration", "MACAddress", "IPEnabled");
        }


标签:WMI,硬件信息,联想台式机,出厂编号,CPUID,BIOS,MAC地址
0
投稿

猜你喜欢

  • 简单了解Android性能优化方向及相关工具

    2022-10-15 19:10:54
  • C#实现图片切割、切图、裁剪

    2022-10-24 15:12:19
  • 提示信息控件AlertDialog对话框详解

    2022-04-18 22:20:45
  • 详解基于Spring Cloud几行配置完成单点登录开发

    2023-07-06 19:14:11
  • MyBatis传入数组集合类并使用foreach遍历

    2022-04-19 02:25:00
  • 浅谈JAVA如何生成UUID唯一标识

    2023-08-12 20:45:03
  • C#中dynamic关键字的正确用法(推荐)

    2023-11-12 09:20:22
  • Android实现ListView分页加载数据

    2023-11-09 10:05:45
  • C#中的虚方法和抽象方法的运用

    2023-06-02 15:49:40
  • Java源码深度分析String与StringBuffer及StringBuilder详解

    2022-04-01 09:55:50
  • SpringBoot 转发请求至指定页面的操作方法

    2022-11-13 17:23:12
  • Android实现通用筛选栏

    2021-10-16 05:31:42
  • Flutter实现切换应用时隐藏应用预览

    2021-07-20 21:05:46
  • 基于java配置nginx获取真实IP代码实例

    2023-07-15 12:32:46
  • Spring案例打印机的实现过程详解

    2023-12-02 12:28:47
  • Android 四种动画效果的调用实现代码

    2021-06-26 17:59:54
  • C#在复杂多线程环境下使用读写锁同步写入文件

    2021-06-16 03:45:53
  • 最最常用的 100 个 Java类分享

    2023-08-21 08:45:05
  • Java二分查找算法实现代码实例

    2023-08-24 05:34:23
  • C语言函数封装及变量的作用域

    2022-10-06 10:14:47
  • asp之家 软件编程 m.aspxhome.com