using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Management; using Microsoft.Win32; using Microsoft.VisualBasic.Devices; using System.Windows.Forms; using System.Drawing; namespace PMS.Common { public static class GetMachineInfo { /// <summary> /// 取机器名 /// </summary> /// <returns></returns> public static string GethostName() { return System.Net.Dns.GetHostName(); } /// <summary> /// 获取cpu序列号 /// </summary> /// <returns></returns> public static string GetCPUSerialNumber() { string cpuSerialNumber = ""; try { ManagementClass mc = new ManagementClass("Win32_Processor"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { cpuSerialNumber = mo["ProcessorId"].ToString(); break; } mc.Dispose(); moc.Dispose(); } catch { } return cpuSerialNumber.Replace("-", "").Replace(" ", "").Replace(":", ""); } /// <summary> /// 获取硬盘序列号 /// </summary> /// <returns></returns> public static string GetDiskSerialNumber() { string strID = ""; try { ManagementClass mc = new ManagementClass("Win32_PhysicalMedia"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { strID = mo.Properties["SerialNumber"].Value.ToString(); break; } } catch { } return strID.Replace("-", "").Replace(" ", "").Replace(":", ""); } /// <summary> /// 获取网卡硬件地址 /// </summary> /// <returns></returns> public static string GetMacAddress() { string MacAddress = ""; try { using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration")) { ManagementObjectCollection moc2 = mc.GetInstances(); foreach (ManagementObject mo in moc2) { if ((bool)mo["IPEnabled"] == true) { MacAddress = mo["MacAddress"].ToString(); } } } } catch { } return MacAddress.Replace("-", "").Replace(" ", "").Replace(":", ""); } /// <summary> /// 内存 /// </summary> /// <returns></returns> public static string GetMemory() { string st = ""; try { ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { st = mo["TotalPhysicalMemory"].ToString(); } moc = null; mc = null; } catch { } return st.Replace("-", "").Replace(" ", "").Replace(":", ""); } /// <summary> /// 读取BIOS /// </summary> /// <returns></returns> public static string GetBIOS() { string strID = ""; try { ManagementClass mc = new ManagementClass("Win32_BIOS"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { strID = mo.Properties["SerialNumber"].Value.ToString(); break; } } catch { } return strID.Replace("-", "").Replace(" ", "").Replace(":", ""); } /// <summary> /// 获取操作系统版本 /// </summary> /// <returns></returns> public static string GetOS() { return new ComputerInfo().OSFullName; } /// <summary> /// 获取分辨率 /// </summary> /// <returns></returns> public static string GetOsResolution() { Rectangle rect = Screen.PrimaryScreen.Bounds; int width = rect.Width; int height = rect.Height; return width + "*" + height; } /// <summary> /// 获取当前显示器的分辨率 /// </summary> /// <returns></returns> public static string GetOsMemory() { double d = Convert.ToDouble(1024); double result = new ComputerInfo().TotalPhysicalMemory / d / d / d; return Math.Round(result) + "G"; } } }
获取硬件信息时,请优先使用命名空间Microsoft.VisualBasic.Devices(需要导入)下的ComputerInfo类
版权声明:本文为博主原创文章,未经博主允许不得转载。