C# 获取PC Hardware 信息
借鉴于网络
1 /* 2 * Created by 3 * User: 4 * Date: 2022-06-06 5 * Time: 21: 16 6 * 7 * 8 */ 9 using System; 10 using System.Collections.Generic; 11 using System.Linq; 12 using System.Text; 13 using System.Threading.Tasks; 14 using System.Management; 15 using System.Net; 16 using System.IO; 17 18 namespace PCHWINFOR 19 { 20 class Program 21 { 22 public static void Main(string[] args) 23 { 24 // TODO: Implement Functionality Here 25 Recordlog(); 26 //Console.ReadKey(true); 27 } 28 public static void Recordlog() 29 { 30 string Folder = null; 31 string logName = null; 32 string path = null; 33 34 try { 35 ManagementClass mc = new ManagementClass("Win32_BIOS"); 36 ManagementObjectCollection moc = mc.GetInstances(); 37 ManagementClass mc2 = new ManagementClass("Win32_NetworkAdapterConfiguration"); 38 ManagementObjectCollection moc2 = mc2.GetInstances(); 39 foreach (ManagementObject mo in moc) { 40 logName = mo.Properties["SerialNumber"].Value.ToString()+@"_"; 41 } 42 43 foreach (ManagementObject mo2 in moc2) { 44 if ((bool)mo2["IPEnabled"] == true) { 45 string mac = ""; 46 mac = mo2["MacAddress"].ToString(); 47 mac = mac.Replace(":", ""); 48 logName += mac; 49 break; 50 } 51 } 52 53 54 } catch { 55 Console.WriteLine("Error"); 56 } 57 58 Folder = Environment.CurrentDirectory + @"\Logs"; 59 path = Folder + @"\" + logName+@".txt"; 60 FileStream _file = new FileStream(path, FileMode.Create, FileAccess.ReadWrite,FileShare.ReadWrite); 61 StreamWriter SW=new StreamWriter(_file); 62 Console.SetOut(SW); 63 PCHW pchw = new PCHW(); 64 SW.Flush(); 65 SW.Close(); 66 System.Diagnostics.Process.Start(path); 67 } 68 } 69 70 /// <summary> 71 /// Get the PC hardware infomation 72 /// </summary> 73 class PCHW 74 { 75 public PCHW() 76 { 77 78 CpuInfo(); 79 MainBoardInfo(); 80 DiskDriveInfo(); 81 GetDefaultIP(); 82 OsInfo(); 83 BIOSInfo(); 84 MEMInfo(); 85 86 } 87 88 89 /// <summary> 90 /// Cpu信息 91 /// </summary> 92 /// <returns></returns> 93 public void CpuInfo() 94 { 95 96 Console.WriteLine("The CPU infomation: "); 97 try { 98 ManagementClass mc = new ManagementClass(WMIPath.Win32_Processor.ToString()); 99 ManagementObjectCollection moc = mc.GetInstances(); 100 foreach (ManagementObject mo in moc) { 101 Console.WriteLine("\t\tID:" + mo.Properties["ProcessorId"].Value); 102 Console.WriteLine("\t\tName:" + mo.Properties["Name"].Value); 103 Console.WriteLine("\t\tStatus:" + mo.Properties["Status"].Value); 104 Console.WriteLine("\t\tExtClock:" + mo.Properties["ExtClock"].Value+"Ghz"); 105 Console.WriteLine("\t\tMaxClockSpeed:" + mo.Properties["MaxClockSpeed"].Value+"Ghz"); 106 Console.WriteLine("\t\tNumberOfCores:" + mo.Properties["NumberOfCores"].Value); 107 108 } 109 } catch { 110 Console.WriteLine("Erroe"); 111 } 112 113 } 114 115 /// <summary> 116 /// 主板信息 117 /// </summary> 118 public void MainBoardInfo() 119 { 120 121 122 Console.WriteLine("\nThe mainboard infomation: "); 123 124 try { 125 ManagementClass mc = new ManagementClass(WMIPath.Win32_BaseBoard.ToString()); 126 ManagementObjectCollection moc = mc.GetInstances(); 127 foreach (ManagementObject mo in moc) { 128 Console.WriteLine("\t\tManufacturer:" + mo.Properties["Manufacturer"].Value); 129 Console.WriteLine("\t\tProduct:" + mo.Properties["Product"].Value); 130 Console.WriteLine("\t\tVersion:" + mo.Properties["Version"].Value); 131 } 132 } catch { 133 Console.WriteLine("Error"); 134 } 135 } 136 137 /// <summary> 138 /// 硬盘信息 139 /// </summary> 140 public void DiskDriveInfo() 141 { 142 143 Console.WriteLine("\nThe DiskDriver infomation: "); 144 try { 145 ManagementClass mc = new ManagementClass(WMIPath.Win32_DiskDrive.ToString()); 146 ManagementObjectCollection moc = mc.GetInstances(); 147 foreach (ManagementObject mo in moc) { 148 Console.WriteLine("\n\t\tSN:" + mo.Properties["SerialNumber"].Value); 149 Console.WriteLine("\t\tType:" + mo.Properties["Model"].Value); 150 Console.WriteLine("\t\tInterfaceType:" + mo.Properties["InterfaceType"].Value); 151 Console.WriteLine("\t\tType:" + mo.Properties["Model"].Value); 152 Console.WriteLine("\t\tSize:" + Convert.ToDouble(mo.Properties["Size"].Value) / (1000 * 1000 * 1000)+"G"); 153 } 154 } catch { 155 Console.WriteLine("Error"); 156 } 157 } 158 159 /// <summary> 160 /// 获取当前服务器或本地电脑的默认ip信息 161 /// </summary> 162 /// <returns></returns> 163 public void GetDefaultIP() 164 { 165 166 Console.WriteLine("\nThe all IP infomation: "); 167 try { 168 IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName()); 169 //IPAddress ipAddr = ipHost.AddressList[0]; 170 //Console.WriteLine("本机IP地址:" + ipAddr.ToString()); 171 for (int index = 0; index < ipHost.AddressList.Length; index++) { 172 Console.WriteLine("\t\t"+ipHost.AddressList[index]); 173 } 174 } catch (Exception e) { 175 Console.WriteLine("{0} Exception caught.", e); 176 } 177 } 178 /// <summary> 179 /// 操作系统信息 180 /// </summary> 181 public void OsInfo() 182 { 183 Console.WriteLine("\nThe OS infomation: "); 184 try { 185 ManagementClass mc = new ManagementClass(WMIPath.Win32_OperatingSystem.ToString()); 186 ManagementObjectCollection moc = mc.GetInstances(); 187 foreach (ManagementObject mo in moc) { 188 Console.WriteLine("\t\tName:" + mo.Properties["Caption"].Value); 189 Console.WriteLine("\t\tVersion:" + mo.Properties["Version"].Value); 190 Console.WriteLine("\t\tDir:" + mo.Properties["SystemDirectory"].Value); 191 } 192 } catch { 193 Console.WriteLine("Erroe"); 194 } 195 } 196 197 /// <summary> 198 /// BIOS 199 /// </summary> 200 public void BIOSInfo() 201 { 202 Console.WriteLine("\nThe BIOS infomation: "); 203 204 try { 205 ManagementClass mc = new ManagementClass(WMIPath.Win32_BIOS.ToString()); 206 ManagementObjectCollection moc = mc.GetInstances(); 207 foreach (ManagementObject mo in moc) { 208 Console.WriteLine("\t\tManufacturer:" + mo.Properties["Manufacturer"].Value); 209 Console.WriteLine("\t\tName:" + mo.Properties["Name"].Value); 210 Console.WriteLine("\t\tSerialNumber:" + mo.Properties["SerialNumber"].Value); 211 Console.WriteLine("\t\tReleaseDate:" + mo.Properties["ReleaseDate"].Value); 212 } 213 } catch { 214 Console.WriteLine("Error"); 215 } 216 } 217 218 /// <summary> 219 /// Memory 220 /// </summary> 221 public void MEMInfo() 222 { 223 Console.WriteLine("\nThe memory infomation: "); 224 225 try { 226 ManagementClass mc = new ManagementClass(WMIPath.Win32_PhysicalMemory.ToString()); 227 ManagementObjectCollection moc = mc.GetInstances(); 228 foreach (ManagementObject mo in moc) { 229 Console.WriteLine("\t\tManufacturer:" + mo.Properties["Manufacturer"].Value); 230 Console.WriteLine("\t\tCapacity: " + Convert.ToDouble(mo.Properties["Capacity"].Value) / (1024 * 1024 * 1024) + "G"); 231 Console.WriteLine("\t\tPartNumber:" + mo.Properties["PartNumber"].Value); 232 Console.WriteLine("\t\tMaxVoltage:" + mo.Properties["MaxVoltage"].Value); 233 Console.WriteLine("\t\tMinVoltage:" + mo.Properties["MinVoltage"].Value); 234 Console.WriteLine("\t\tSpeed:" + mo.Properties["Speed"].Value+"Ghz"); 235 Console.WriteLine("\t\tSN:" + mo.Properties["SerialNumber"].Value); 236 Console.WriteLine("\t\tTypeDetail:" + mo.Properties["TypeDetail"].Value); 237 } 238 } catch { 239 Console.WriteLine("Error"); 240 } 241 } 242 } 243 public enum WMIPath 244 { 245 // 硬件 246 Win32_Processor, 247 // CPU 处理器 248 Win32_PhysicalMemory, 249 // 物理内存条 250 Win32_Keyboard, 251 // 键盘 252 Win32_PointingDevice, 253 // 点输入设备,包括鼠标。 254 Win32_FloppyDrive, 255 // 软盘驱动器 256 Win32_DiskDrive, 257 // 硬盘驱动器 258 Win32_CDROMDrive, 259 // 光盘驱动器 260 Win32_BaseBoard, 261 // 主板 262 Win32_BIOS, 263 // BIOS 芯片 264 Win32_ParallelPort, 265 // 并口 266 Win32_SerialPort, 267 // 串口 268 Win32_SerialPortConfiguration, 269 // 串口配置 270 Win32_SoundDevice, 271 // 多媒体设置,一般指声卡。 272 Win32_SystemSlot, 273 // 主板插槽 (ISA & PCI & AGP) 274 Win32_USBController, 275 // USB 控制器 276 Win32_NetworkAdapter, 277 // 网络适配器 278 Win32_NetworkAdapterConfiguration, 279 // 网络适配器设置 280 Win32_Printer, 281 // 打印机 282 Win32_PrinterConfiguration, 283 // 打印机设置 284 Win32_PrintJob, 285 // 打印机任务 286 Win32_TCPIPPrinterPort, 287 // 打印机端口 288 Win32_POTSModem, 289 // MODEM 290 Win32_POTSModemToSerialPort, 291 // MODEM 端口 292 Win32_DesktopMonitor, 293 // 显示器 294 Win32_DisplayConfiguration, 295 // 显卡 296 Win32_DisplayControllerConfiguration, 297 // 显卡设置 298 Win32_VideoController, 299 // 显卡细节。 300 Win32_VideoSettings, 301 // 显卡支持的显示模式。 302 303 // 操作系统 304 Win32_TimeZone, 305 // 时区 306 Win32_SystemDriver, 307 // 驱动程序 308 Win32_DiskPartition, 309 // 磁盘分区 310 Win32_LogicalDisk, 311 // 逻辑磁盘 312 Win32_LogicalDiskToPartition, 313 // 逻辑磁盘所在分区及始末位置。 314 Win32_LogicalMemoryConfiguration, 315 // 逻辑内存配置 316 Win32_PageFile, 317 // 系统页文件信息 318 Win32_PageFileSetting, 319 // 页文件设置 320 Win32_BootConfiguration, 321 // 系统启动配置 322 Win32_ComputerSystem, 323 // 计算机信息简要 324 Win32_OperatingSystem, 325 // 操作系统信息 326 Win32_StartupCommand, 327 // 系统自动启动程序 328 Win32_Service, 329 // 系统安装的服务 330 Win32_Group, 331 // 系统管理组 332 Win32_GroupUser, 333 // 系统组帐号 334 Win32_UserAccount, 335 // 用户帐号 336 Win32_Process, 337 // 系统进程 338 Win32_Thread, 339 // 系统线程 340 Win32_Share, 341 // 共享 342 Win32_NetworkClient, 343 // 已安装的网络客户端 344 Win32_NetworkProtocol, 345 // 已安装的网络协议 346 347 348 } 349 }