码迷,mamicode.com
首页 > 其他好文 > 详细

获取硬件信息

时间:2015-08-05 17:41:53      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:

感觉话说的多了,费劲。

    public class ComputerInfo
    {
        public ComputerInfo()
        {}

         /// <summary>
        /// cpu型号
        /// </summary>
        /// <returns></returns>
        public string GetCpuInfo()
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"root\CIMV2", "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"; } } }

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

获取硬件信息

标签:

原文地址:http://www.cnblogs.com/JeffConrtoller/p/4705047.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!