using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Management;namespace IPMACDemo{ class Program { static void Main(string[] args) { Console.WriteLine("计算机名称:{0}",GetComputerName()); Console.WriteLine("操作系统:{0}", GetSystemType()); Console.WriteLine("用户名:{0}", GetUserName()); Console.WriteLine("计算机MAC地址:{0}", GetMacAddress()); Console.WriteLine("计算机IP地址:{0}", GetIpAddress()); Console.WriteLine("硬盘序列号:{0}", GetDiskSerialNumber()); Console.WriteLine("硬盘大小:{0}", Convert.ToDouble(GetSizeOfDisk()) /(1024*1024*1024)); Console.WriteLine("网卡地址:{0}", GetMacAddress()); Console.WriteLine("显卡PNPDeviceID:{0}", GetVideoPnpid()); Console.WriteLine("声卡PNPDeviceID :{0}", GetSoundPnpid()); Console.WriteLine("主板制造商:{0}", GetBoardManufacturer()); Console.WriteLine("主板编号:{0}", GetBoardId()); Console.WriteLine("主板型号:{0}", GetBoardType()); Console.WriteLine("CPU名称:{0}", GetCpuName()); Console.WriteLine("CPU数量:{0}", GetCpuCount()); Console.WriteLine("CPU编号:{0}", GetCpuid()); Console.WriteLine("CPU版本信息:{0}", GetCpuVersion()); Console.WriteLine("CPU制造商:{0}", GetCpuManufacturer()); Console.WriteLine("物理内存:{0}", Convert.ToDouble(GetPhysicalMemory()) / (1024 * 1024 * 1024)); Console.ReadLine(); } #region CPU ////// 获取CPU的频率 这里之所以使用string类型的数组,主要是因为cpu的多核 /// ///public static string[] GetCpuMHZ() { ManagementClass mc = new ManagementClass("Win32_Processor"); ManagementObjectCollection cpus = mc.GetInstances(); string[] mHz = new string[cpus.Count]; int c = 0; ManagementObjectSearcher mySearch = new ManagementObjectSearcher("select * from Win32_Processor"); foreach (ManagementObject mo in mySearch.Get()) { mHz[c] = mo.Properties["CurrentClockSpeed"].Value.ToString(); c++; } mc.Dispose(); mySearch.Dispose(); return mHz; } /// /// 获取CPU的个数 /// ///public static int GetCpuCount() { try { using (ManagementClass mCpu = new ManagementClass("Win32_Processor")) { ManagementObjectCollection cpus = mCpu.GetInstances(); return cpus.Count; } } catch { } return -1; } /// /// 获得CPU编号 /// ///public static string GetCpuid() { var cpuid = string.Empty; var mc = new ManagementClass("Win32_Processor"); var moc = mc.GetInstances(); foreach (var o in moc) { var mo = (ManagementObject)o; cpuid = mo.Properties["ProcessorId"].Value.ToString(); } return cpuid; } /// /// CPU版本信息 /// ///public static string GetCpuVersion() { var st = string.Empty; var mos = new ManagementObjectSearcher("Select * from Win32_Processor"); foreach (var o in mos.Get()) { var mo = (ManagementObject)o; st = mo["Version"].ToString(); } return st; } /// /// CPU名称信息 /// ///public static string GetCpuName() { var st = string.Empty; var driveId = new ManagementObjectSearcher("Select * from Win32_Processor"); foreach (var o in driveId.Get()) { var mo = (ManagementObject)o; st = mo["Name"].ToString(); } return st; } /// /// CPU制造厂商 /// ///public static string GetCpuManufacturer() { var st = string.Empty; var mos = new ManagementObjectSearcher("Select * from Win32_Processor"); foreach (var o in mos.Get()) { var mo = (ManagementObject)o; st = mo["Manufacturer"].ToString(); } return st; } #endregion #region 主板 /// /// 主板制造厂商 /// ///public static string GetBoardManufacturer() { var query = new SelectQuery("Select * from Win32_BaseBoard"); var mos = new ManagementObjectSearcher(query); var data = mos.Get().GetEnumerator(); data.MoveNext(); var board = data.Current; return board.GetPropertyValue("Manufacturer").ToString(); } /// /// 主板编号 /// ///public static string GetBoardId() { var st = string.Empty; var mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard"); foreach (var o in mos.Get()) { var mo = (ManagementObject)o; st = mo["SerialNumber"].ToString(); } return st; } /// /// 主板型号 /// ///public static string GetBoardType() { var st = string.Empty; var mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard"); foreach (var o in mos.Get()) { var mo = (ManagementObject)o; st = mo["Product"].ToString(); } return st; } #endregion #region 网卡,声卡,显卡 /// /// 获取网卡硬件地址 /// ///public static string GetMacAddress() { var mac = ""; var mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); var moc = mc.GetInstances(); foreach (var o in moc) { var mo = (ManagementObject)o; if (!(bool)mo["IPEnabled"]) continue; mac = mo["MacAddress"].ToString(); break; } return mac; } /// /// 显卡PNPDeviceID /// ///public static string GetVideoPnpid() { var st = ""; var mos = new ManagementObjectSearcher("Select * from Win32_VideoController"); foreach (var o in mos.Get()) { var mo = (ManagementObject)o; st = mo["PNPDeviceID"].ToString(); } return st; } /// /// 声卡PNPDeviceID /// ///public static string GetSoundPnpid() { var st = string.Empty; var mos = new ManagementObjectSearcher("Select * from Win32_SoundDevice"); foreach (var o in mos.Get()) { var mo = (ManagementObject)o; st = mo["PNPDeviceID"].ToString(); } return st; } #endregion /// /// 获取硬盘序列号 /// ///public static string GetDiskSerialNumber() { //这种模式在插入一个U盘后可能会有不同的结果,如插入我的手机时 var hDid = string.Empty; var mc = new ManagementClass("Win32_DiskDrive"); var moc = mc.GetInstances(); foreach (var o in moc) { var mo = (ManagementObject)o; hDid = (string)mo.Properties["Model"].Value; //这名话解决有多个物理盘时产生的问题,只取第一个物理硬盘 break; } return hDid; } /// /// 获取硬盘的大小 /// ///public static string GetSizeOfDisk() { ManagementClass mc = new ManagementClass("Win32_DiskDrive"); ManagementObjectCollection moj = mc.GetInstances(); foreach (ManagementObject m in moj) { return m.Properties["Size"].Value.ToString(); } return "-1"; } /// /// 物理内存 /// ///public static string GetPhysicalMemory() { var st = string.Empty; var mc = new ManagementClass("Win32_ComputerSystem"); var moc = mc.GetInstances(); foreach (var o in moc) { var mo = (ManagementObject)o; st = mo["TotalPhysicalMemory"].ToString(); } return st; } /// /// 获取IP地址 /// ///public static string GetIpAddress() { var st = string.Empty; var mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); var moc = mc.GetInstances(); foreach (var o in moc) { var mo = (ManagementObject)o; if (!(bool)mo["IPEnabled"]) continue; var ar = (Array)(mo.Properties["IpAddress"].Value); st = ar.GetValue(0).ToString(); break; } return st; } /// /// 操作系统的登录用户名 /// ///public static string GetUserName() { return Environment.UserName; } /// /// 获取计算机名 /// ///public static string GetComputerName() { return Environment.MachineName; } /// /// 操作系统类型 /// ///public static string GetSystemType() { var st = string.Empty; var mc = new ManagementClass("Win32_ComputerSystem"); var moc = mc.GetInstances(); foreach (var o in moc) { var mo = (ManagementObject)o; st = mo["SystemType"].ToString(); } return st; } }}