zoukankan      html  css  js  c++  java
  • 获取硬件信息

    感觉话说的多了,费劲。

        public class ComputerInfo
        {
            public ComputerInfo()
            {}
    
             /// <summary>
            /// cpu型号
            /// </summary>
            /// <returns></returns>
            public string GetCpuInfo()
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"rootCIMV2", "SELECT * FROM Win32_Processor");
                foreach (ManagementObject obj2 in searcher.Get())
                {
                    try
                    {
                        return (obj2.GetPropertyValue("Name").ToString() + "," + obj2.GetPropertyValue("CurrentClockSpeed").ToString() + " Mhz," + Environment.ProcessorCount.ToString() + " 个处理器");
                    }
                    catch
                    {
                        continue;
                    }
                }
                return "未知";
            }
            /// <summary>
            /// cpuID
            /// </summary>
            /// <returns></returns>
            public string GetCpuID()
            {
                ManagementObjectCollection instances = new ManagementClass("Win32_Processor").GetInstances();
                foreach (ManagementObject obj2 in instances)
                {
                    return obj2.Properties["ProcessorId"].Value.ToString();
                }
                return null;
            }
    
            /// <summary>
            /// 取得设备硬盘的卷标号
            /// </summary>
            /// <returns></returns>
            public string GetDiskVolumeSerialNumber()
            {
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid="c:"");
                disk.Get();
                return disk.GetPropertyValue("VolumeSerialNumber").ToString();
            }
    
            /// <summary>
            /// 获得CPU的序列号
            /// </summary>
            /// <returns></returns>
            public string getCpu()
            {
                string strCpu = null;
                ManagementClass myCpu = new ManagementClass("win32_Processor");
                ManagementObjectCollection myCpuConnection = myCpu.GetInstances();
                foreach (ManagementObject myObject in myCpuConnection)
                {
                    strCpu = myObject.Properties["Processorid"].Value.ToString();
                    break;
                }
                return strCpu;
            }
    
            /// <summary>
            /// 生成机器码
            /// </summary>
            /// <returns></returns>
            public string getMNum()
            {
                string strNum = getCpu() + GetDiskVolumeSerialNumber();//获得24位Cpu和硬盘序列号
                string strMNum = strNum.Substring(0, 24);//从生成的字符串中取出前24个字符做为机器码
                return strMNum;
            }

    /// <summary> /// 物理内存 /// </summary> /// <returns></returns> public string GetTotalPhysicalMemory() { try { string st = ""; ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { st = mo.ToString(); } moc = null; mc = null; return st; } catch { return "unknow"; } } /// <summary> /// 获取计算机名(ComputerName) /// </summary> /// <returns></returns> public string GetComputerName() { try { return System.Environment.GetEnvironmentVariable("ComputerName"); } catch { return "unknow"; } } /// <summary> /// PC类型 /// </summary> /// <returns></returns> public string GetSystemType() { try { string st = ""; ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { st = mo.ToString(); } moc = null; mc = null; return st; } catch { return "unknow"; } } /// <summary> /// 操作系统的登录用户名 /// </summary> /// <returns></returns> public string GetUserName() { try { string st = ""; ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { st = mo.ToString(); } moc = null; mc = null; return st; } catch { return "unknow"; } } }

    备注写的很清楚,只是做一个存档。

  • 相关阅读:
    【Appium】appium踩坑记录:解决每次安装appium setting和Unlock
    Pycharm from XXX import XXX 引入本地文件标红报错(source_path&Python package)
    🍖02 不同平台更换pip源
    🍖pycharm 更换 pip 下载源
    🍖01 路飞学城项目分析
    🍖Vue-cli 创建项目
    🍖Vue 与后端交互
    🍖Vue 计算属性
    🍖Vue 虚拟DOM与Diff算法简介
    🍖Vue 生命期钩子
  • 原文地址:https://www.cnblogs.com/JeffController/p/4705047.html
Copyright © 2011-2022 走看看