C# 获取计算机的硬件信息
1 /// <summary> 2 /// 获得CPU编号 3 /// </summary> 4 /// <returns></returns> 5 public string GetCPUID() 6 { 7 string cpuid = ""; 8 ManagementClass mc = new ManagementClass("Win32_Processor"); 9 ManagementObjectCollection moc = mc.GetInstances(); 10 foreach (ManagementObject mo in moc) 11 { 12 cpuid = mo.Properties["ProcessorId"].Value.ToString(); 13 } 14 return cpuid; 15 } 16 17 /// <summary> 18 /// 获取硬盘序列号 19 /// </summary> 20 /// <returns></returns> 21 public string GetDiskSerialNumber() 22 { 23 //这种模式在插入一个U盘后可能会有不同的结果,如插入我的手机时 24 String HDid = ""; 25 ManagementClass mc = new ManagementClass("Win32_DiskDrive"); 26 ManagementObjectCollection moc = mc.GetInstances(); 27 foreach (ManagementObject mo in moc) 28 { 29 HDid = (string)mo.Properties["Model"].Value;//SerialNumber 30 break;//这名话解决有多个物理盘时产生的问题,只取第一个物理硬盘 31 } 32 return HDid; 33 34 /*ManagementClass mc = new ManagementClass("Win32_PhysicalMedia"); 35 ManagementObjectCollection moc = mc.GetInstances(); 36 string str = ""; 37 foreach (ManagementObject mo in moc) 38 { 39 str = mo.Properties["SerialNumber"].Value.ToString(); 40 break; 41 } 42 return str;*/ 43 } 44 45 /// <summary> 46 /// 获取网卡硬件地址 47 /// </summary> 48 /// <returns></returns> 49 public string GetMacAddress() 50 { 51 string mac = ""; 52 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 53 ManagementObjectCollection moc = mc.GetInstances(); 54 foreach (ManagementObject mo in moc) 55 { 56 if ((bool)mo["IPEnabled"] == true) 57 { 58 mac = mo["MacAddress"].ToString(); 59 break; 60 } 61 } 62 return mac; 63 } 64 65 /// <summary> 66 /// 获取IP地址 67 /// </summary> 68 /// <returns></returns> 69 public string GetIPAddress() 70 { 71 string st = ""; 72 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 73 ManagementObjectCollection moc = mc.GetInstances(); 74 foreach (ManagementObject mo in moc) 75 { 76 if ((bool)mo["IPEnabled"] == true) 77 { 78 //st=mo["IpAddress"].ToString(); 79 System.Array ar; 80 ar = (System.Array)(mo.Properties["IpAddress"].Value); 81 st = ar.GetValue(0).ToString(); 82 break; 83 } 84 } 85 return st; 86 } 87 88 /// <summary> 89 /// 操作系统的登录用户名 90 /// </summary> 91 /// <returns></returns> 92 public string GetUserName() 93 { 94 return Environment.UserName; 95 } 96 97 98 /// <summary> 99 /// 获取计算机名 100 /// </summary> 101 /// <returns></returns> 102 public string GetComputerName() 103 { 104 return Environment.MachineName; 105 } 106 107 /// <summary> 108 /// 操作系统类型 109 /// </summary> 110 /// <returns></returns> 111 public string GetSystemType() 112 { 113 string st = ""; 114 ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); 115 ManagementObjectCollection moc = mc.GetInstances(); 116 foreach (ManagementObject mo in moc) 117 { 118 st = mo["SystemType"].ToString(); 119 } 120 return st; 121 } 122 123 /// <summary> 124 /// 物理内存 125 /// </summary> 126 /// <returns></returns> 127 public string GetPhysicalMemory() 128 { 129 string st = ""; 130 ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); 131 ManagementObjectCollection moc = mc.GetInstances(); 132 foreach (ManagementObject mo in moc) 133 { 134 st = mo["TotalPhysicalMemory"].ToString(); 135 } 136 return st; 137 } 138 139 /// <summary> 140 /// 显卡PNPDeviceID 141 /// </summary> 142 /// <returns></returns> 143 public string GetVideoPNPID() 144 { 145 string st = ""; 146 ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_VideoController"); 147 foreach (ManagementObject mo in mos.Get()) 148 { 149 st = mo["PNPDeviceID"].ToString(); 150 } 151 return st; 152 } 153 154 /// <summary> 155 /// 声卡PNPDeviceID 156 /// </summary> 157 /// <returns></returns> 158 public string GetSoundPNPID() 159 { 160 string st = ""; 161 ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_SoundDevice"); 162 foreach (ManagementObject mo in mos.Get()) 163 { 164 st = mo["PNPDeviceID"].ToString(); 165 } 166 return st; 167 } 168 169 /// <summary> 170 /// CPU版本信息 171 /// </summary> 172 /// <returns></returns> 173 public string GetCPUVersion() 174 { 175 string st = ""; 176 ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_Processor"); 177 foreach (ManagementObject mo in mos.Get()) 178 { 179 st = mo["Version"].ToString(); 180 } 181 return st; 182 } 183 184 /// <summary> 185 /// CPU名称信息 186 /// </summary> 187 /// <returns></returns> 188 public string GetCPUName() 189 { 190 string st = ""; 191 ManagementObjectSearcher driveID = new ManagementObjectSearcher("Select * from Win32_Processor"); 192 foreach (ManagementObject mo in driveID.Get()) 193 { 194 st = mo["Name"].ToString(); 195 } 196 return st; 197 } 198 199 /// <summary> 200 /// CPU制造厂商 201 /// </summary> 202 /// <returns></returns> 203 public string GetCPUManufacturer() 204 { 205 string st = ""; 206 ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_Processor"); 207 foreach (ManagementObject mo in mos.Get()) 208 { 209 st = mo["Manufacturer"].ToString(); 210 } 211 return st; 212 } 213 214 /// <summary> 215 /// 主板制造厂商 216 /// </summary> 217 /// <returns></returns> 218 public string GetBoardManufacturer() 219 { 220 SelectQuery query = new SelectQuery("Select * from Win32_BaseBoard"); 221 ManagementObjectSearcher mos = new ManagementObjectSearcher(query); 222 ManagementObjectCollection.ManagementObjectEnumerator data = mos.Get().GetEnumerator(); 223 data.MoveNext(); 224 ManagementBaseObject board = data.Current; 225 return board.GetPropertyValue("Manufacturer").ToString(); 226 } 227 228 /// <summary> 229 /// 主板编号 230 /// </summary> 231 /// <returns></returns> 232 public string GetBoardID() 233 { 234 string st = ""; 235 ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard"); 236 foreach (ManagementObject mo in mos.Get()) 237 { 238 st = mo["SerialNumber"].ToString(); 239 } 240 return st; 241 } 242 243 /// <summary> 244 /// 主板型号 245 /// </summary> 246 /// <returns></returns> 247 public string GetBoardType() 248 { 249 string st = ""; 250 ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard"); 251 foreach (ManagementObject mo in mos.Get()) 252 { 253 st = mo["Product"].ToString(); 254 } 255 return st; 256 }
// 硬件
Win32_Processor, // CPU 处理器
Win32_PhysicalMemory, // 物理内存条
Win32_Keyboard, // 键盘
Win32_PointingDevice, // 点输入设备,包括鼠标。
Win32_FloppyDrive, // 软盘驱动器
Win32_DiskDrive, // 硬盘驱动器
Win32_CDROMDrive, // 光盘驱动器
Win32_BaseBoard, // 主板
Win32_BIOS, // BIOS 芯片
Win32_ParallelPort, // 并口
Win32_SerialPort, // 串口
Win32_SerialPortConfiguration, // 串口配置
Win32_SoundDevice, // 多媒体设置,一般指声卡。
Win32_SystemSlot, // 主板插槽 (ISA & PCI & AGP)
Win32_USBController, // USB 控制器
Win32_NetworkAdapter, // 网络适配器
Win32_NetworkAdapterConfiguration, // 网络适配器设置
Win32_Printer, // 打印机
Win32_PrinterConfiguration, // 打印机设置
Win32_PrintJob, // 打印机任务
Win32_TCPIPPrinterPort, // 打印机端口
Win32_POTSModem, // MODEM
Win32_POTSModemToSerialPort, // MODEM 端口
Win32_DesktopMonitor, // 显示器
Win32_DisplayConfiguration, // 显卡
Win32_DisplayControllerConfiguration, // 显卡设置
Win32_VideoController, // 显卡细节。
Win32_VideoSettings, // 显卡支持的显示模式。
// 操作系统
Win32_TimeZone, // 时区
Win32_SystemDriver, // 驱动程序
Win32_DiskPartition, // 磁盘分区
Win32_LogicalDisk, // 逻辑磁盘
Win32_LogicalDiskToPartition, // 逻辑磁盘所在分区及始末位置。
Win32_LogicalMemoryConfiguration, // 逻辑内存配置
Win32_PageFile, // 系统页文件信息
Win32_PageFileSetting, // 页文件设置
Win32_BootConfiguration, // 系统启动配置
Win32_ComputerSystem, // 计算机信息简要
Win32_OperatingSystem, // 操作系统信息
Win32_StartupCommand, // 系统自动启动程序
Win32_Service, // 系统安装的服务
Win32_Group, // 系统管理组
Win32_GroupUser, // 系统组帐号
Win32_UserAccount, // 用户帐号
Win32_Process, // 系统进程
Win32_Thread, // 系统线程
Win32_Share, // 共享
Win32_NetworkClient, // 已安装的网络客户端
Win32_NetworkProtocol, // 已安装的网络协议