c# 获取系统本身硬件的信息
需要添加 System.Management 程序集
- 获取操作系统名称
ManagementClass manag = new ManagementClass("Win32_OperatingSystem"); ManagementObjectCollection managCollection = manag.GetInstances(); foreach (ManagementObject m in managCollection) {
Console.WriteLine(m["Name"].ToString();
break;
}
2. 获取CPU信息
ManagementClass mcCPU = new ManagementClass("Win32_Processor"); ManagementObjectCollection mocCPU = mcCPU.GetInstances(); foreach (ManagementObject m in mocCPU) { Console.WriteLine(m["Name"].ToString()); break; }
3. 获取CPU核心数量
int coreCount = 0; foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_Processor ").Get()) { coreCount += int.Parse(item["NumberOfCores"].ToString()); }
Console.WriteLine(coreCount.ToString() + "核");
4. 获取系统内存大小
ManagementObjectSearcher searcher = new ManagementObjectSearcher(); //用于查询一些如系统信息的管理对象 searcher.Query = new SelectQuery("Win32_PhysicalMemory", "", new string[] { "Capacity" });//设置查询条件 ManagementObjectCollection collection = searcher.Get(); //获取内存容量 ManagementObjectCollection.ManagementObjectEnumerator em = collection.GetEnumerator(); long capacity = 0; while (em.MoveNext()) { ManagementBaseObject baseObj = em.Current; if (baseObj.Properties["Capacity"].Value != null) { capacity += long.Parse(baseObj.Properties["Capacity"].Value.ToString()); } } Console.WriteLine(capacity / 1024 / 1024 / 1024 + "GB");
4. 获取硬盘空间
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives(); long totalFreeSpace = 0; long totalDiskSize = 0; foreach (var drive in drives) { if (drive.IsReady) //判断代码运行时 磁盘是可操作作态 { totalFreeSpace += drive.AvailableFreeSpace; totalDiskSize += drive.TotalSize; } } Console.WriteLine(string.Format("总容量:{0} 剩余:{1}", totalDiskSize / 1024 / 1024 / 1024 + "GB", totalFreeSpace / 1024 / 1024 / 1024 + "GB"));
5. 获取屏幕分辨率
ManagementClass manage = new ManagementClass("Win32_DesktopMonitor"); ManagementObjectCollection manageCollection = manage.GetInstances(); foreach (ManagementObject m in manageCollection) { Console.WriteLine(m["ScreenWidth"].ToString() + "*" + m["ScreenHeight"].ToString()); break; }
6. 获取显卡芯片名称 与 显存大小
ManagementClass manage = new ManagementClass("Win32_VideoController"); ManagementObjectCollection manageCollection = manage.GetInstances(); foreach (ManagementObject m in manageCollection) { Console.WriteLine(m["VideoProcessor"].ToString().Replace("Family", "")); Console.WriteLine((Convert.ToInt64(m["AdapterRAM"]) / 1024 / 1024 / 1024) + "GB" ); break; }