LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

【C#】获取电脑CPU、内存、屏幕、磁盘等信息

admin
2023年10月23日 16:6 本文热度 590

通过WMI类来获取电脑各种信息,参考文章:

WMI_04_常见的WMI类的属性

下面整理了获取电脑CPU、内存、屏幕、磁盘等信息的代码:

1.    #region 系统信息

2.     

3.        /// <summary>

4.        /// 电脑信息

5.        /// </summary>

6.        public partial class ComputerInfo

7.        {

8.            /// <summary>

9.            /// 系统版本

10.           /// <para>示例:Windows 10 Enterprise</para>

11.           /// </summary>

12.           public static string OSProductName { get; } = (string)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "ProductName", 0);

13.    

14.           /// <summary>

15.           /// 操作系统版本

16.           /// <para>示例:Microsoft Windows 10.0.18363</para>

17.           /// </summary>

18.           public static string OSDescription { get; } = System.Runtime.InteropServices.RuntimeInformation.OSDescription;

19.    

20.           /// <summary>

21.           /// 操作系统架构(<see cref="Architecture">

22.           /// <para>示例:X64</para>

23.           /// </summary>

24.           public static string OSArchitecture { get; } = System.Runtime.InteropServices.RuntimeInformation.OSArchitecture.ToString();

25.    

26.           /// <summary>

27.           /// 获取系统信息

28.           /// </summary>

29.           /// <returns></returns>

30.           public static SystemInfo GetSystemInfo()

31.           {

32.               SystemInfo systemInfo = new SystemInfo();

33.    

34.               var osProductName = OSProductName.Trim().Replace(" ", "_");

35.               var osVersionNames = Enum.GetNames(typeof(OSVersion)).ToList();

36.    

37.               for (int i = 0; i < osVersionNames.Count; i++)

38.               {

39.                   var osVersionName = osVersionNames[i];

40.                   if (osProductName.Contains(osVersionName))

41.                   {

42.                       systemInfo.OSVersion = (OSVersion)Enum.Parse(typeof(OSVersion), osVersionName);

43.                       systemInfo.WindowsVersion = osProductName.Replace(osVersionName, "").Replace("_", " ");

44.                   }

45.               }

46.    

47.               systemInfo.WindowsVersionNo = OSDescription;

48.               systemInfo.Architecture = OSArchitecture;

49.    

50.               return systemInfo;

51.           }

52.       }

53.    

54.       /// <summary>

55.       /// 系统信息

56.       /// </summary>

57.       public class SystemInfo

58.       {

59.           /// <summary>

60.           /// 系统版本。如:Windows 10

61.           /// </summary>

62.           public string WindowsVersion { get; set; }

63.    

64.           /// <summary>

65.           /// 系统版本。如:专业版

66.           /// </summary>

67.           public OSVersion OSVersion { get; set; }

68.           /// <summary>

69.           /// Windows版本号。如:Microsoft Windows 10.0.18363

70.           /// </summary>

71.           public string WindowsVersionNo { get; set; }

72.           /// <summary>

73.           /// 操作系统架构。如:X64

74.           /// </summary>

75.           public string Architecture { get; set; }

76.       }

77.    

78.       /// <summary>

79.       /// 系统客户版本

80.       /// </summary>

81.       public enum OSVersion

82.       {

83.           /// <summary>

84.           /// 家庭版

85.           /// </summary>

86.           Home,

87.           /// <summary>

88.           /// 专业版,以家庭版为基础

89.           /// </summary>

90.           Pro,

91.           Professional,

92.           /// <summary>

93.           /// 企业版,以专业版为基础

94.           /// </summary>

95.           Enterprise,

96.           /// <summary>

97.           /// 教育版,以企业版为基础

98.           /// </summary>

99.           Education,

100.         /// <summary>

101.         /// 移动版

102.         /// </summary>

103.         Mobile,

104.         /// <summary>

105.         /// 企业移动版,以移动版为基础

106.         /// </summary>

107.         Mobile_Enterprise,

108.         /// <summary>

109.         /// 物联网版

110.         /// </summary>

111.         IoT_Core,

112.         /// <summary>

113.         /// 专业工作站版,以专业版为基础

114.         /// </summary>

115.         Pro_for_Workstations

116.     }

117.  

118.     #endregion

119.  

120.     #region CPU信息

121.  

122.     public partial class ComputerInfo

123.     {

124.         /// <summary>

125.         /// CPU信息

126.         /// </summary>

127.         /// <returns></returns>

128.         public static CPUInfo GetCPUInfo()

129.         {

130.             var cpuInfo = new CPUInfo();

131.             var cpuInfoType = cpuInfo.GetType();

132.             var cpuInfoFields = cpuInfoType.GetProperties().ToList();

133.             var moc = new ManagementClass("Win32_Processor").GetInstances();

134.             foreach (var mo in moc)

135.             {

136.                 foreach (var item in mo.Properties)

137.                 {

138.                     if (cpuInfoFields.Exists(f => f.Name == item.Name))

139.                     {

140.                         var p = cpuInfoType.GetProperty(item.Name);

141.                         p.SetValue(cpuInfo, item.Value);

142.                     }

143.                 }

144.             }

145.             return cpuInfo;

146.         }

147.     }

148.  

149.     /// <summary>

150.     /// CPU信息

151.     /// </summary>

152.     public class CPUInfo

153.     {

154.         /// <summary>

155.         /// 操作系统类型,3264

156.         /// </summary>

157.         public uint AddressWidth { get; set; }

158.         /// <summary>

159.         /// 处理器的名称。如:Intel(R) Core(TM) i3-8100 CPU @ 3.60GHz

160.         /// </summary>

161.         public string Name { get; set; }

162.  

163.         /// <summary>

164.         /// 处理器的当前实例的数目。如:44

165.         /// </summary>

166.         public uint NumberOfEnabledCore { get; set; }

167.         /// <summary>

168.         /// 用于处理器的当前实例逻辑处理器的数量。如:44线程

169.         /// </summary>

170.         public uint NumberOfLogicalProcessors { get; set; }

171.         /// <summary>

172.         /// 系统的名称。计算机名称。如:GREAMBWANG

173.         /// </summary>

174.         public string SystemName { get; set; }

175.     }

176.  

177.     #endregion

178.  

179.     #region 内存信息

180.     public partial class ComputerInfo

181.     {

182.         /// <summary>

183.         /// 内存信息

184.         /// </summary>

185.         /// <returns></returns>

186.         public static RAMInfo GetRAMInfo()

187.         {

188.             var ramInfo = new RAMInfo();

189.  

190.             var totalPhysicalMemory = TotalPhysicalMemory;

191.             var memoryAvailable = MemoryAvailable;

192.  

193.             var conversionValue = 1024.0 * 1024.0 * 1024.0;

194.  

195.             ramInfo.TotalPhysicalMemoryGBytes = Math.Round((double)(totalPhysicalMemory / conversionValue), 1);

196.             ramInfo.MemoryAvailableGBytes = Math.Round((double)(memoryAvailable / conversionValue), 1);

197.             ramInfo.UsedMemoryGBytes = Math.Round((double)((totalPhysicalMemory - memoryAvailable) / conversionValue), 1);

198.             ramInfo.UsedMemoryRatio = (int)(((totalPhysicalMemory - memoryAvailable) * 100.0) / totalPhysicalMemory);

199.  

200.             return ramInfo;

201.         }

202.  

203.         /// <summary>

204.         /// 总物理内存(B

205.         /// </summary>

206.         /// <returns></returns>

207.         public static long TotalPhysicalMemory

208.         {

209.             get

210.             {

211.                 long totalPhysicalMemory = 0;

212.  

213.                 ManagementClass mc = new ManagementClass("Win32_ComputerSystem");

214.                 ManagementObjectCollection moc = mc.GetInstances();

215.                 foreach (ManagementObject mo in moc)

216.                 {

217.                     if (mo["TotalPhysicalMemory"] != null)

218.                     {

219.                         totalPhysicalMemory = long.Parse(mo["TotalPhysicalMemory"].ToString());

220.                     }

221.                 }

222.                 return totalPhysicalMemory;

223.             }

224.         }

225.  

226.         /// <summary>

227.         /// 获取可用内存(B

228.         /// </summary>

229.         public static long MemoryAvailable

230.         {

231.             get

232.             {

233.                 long availablebytes = 0;

234.                 ManagementClass mos = new ManagementClass("Win32_OperatingSystem");

235.                 foreach (ManagementObject mo in mos.GetInstances())

236.                 {

237.                     if (mo["FreePhysicalMemory"] != null)

238.                     {

239.                         availablebytes = 1024 * long.Parse(mo["FreePhysicalMemory"].ToString());

240.                     }

241.                 }

242.                 return availablebytes;

243.             }

244.         }

245.  

246.     }

247.  

248.     /// <summary>

249.     /// 内存信息

250.     /// </summary>

251.     public class RAMInfo

252.     {

253.         /// <summary>

254.         /// 总物理内存(GB

255.         /// </summary>

256.         public double TotalPhysicalMemoryGBytes { get; set; }

257.         /// <summary>

258.         /// 获取可用内存(GB

259.         /// </summary>

260.         public double MemoryAvailableGBytes { get; set; }

261.         /// <summary>

262.         /// 获取已用内存(GB

263.         /// </summary>

264.         public double UsedMemoryGBytes { get; set; }

265.         /// <summary>

266.         /// 内存使用率

267.         /// </summary>

268.         public int UsedMemoryRatio { get; set; }

269.     }

270.  

271.     #endregion

272.  

273.     #region 屏幕信息

274.  

275.     public partial class ComputerInfo

276.     {

277.         /// <summary>

278.         /// 屏幕信息

279.         /// </summary>

280.         public static GPUInfo GetGPUInfo()

281.         {

282.             GPUInfo gPUInfo = new GPUInfo();

283.             gPUInfo.CurrentResolution = MonitorHelper.GetResolution();

284.             gPUInfo.MaxScreenResolution = GetGPUInfo2();

285.  

286.             return gPUInfo;

287.         }

288.  

289.         /// <summary>

290.         /// 获取最大分辨率

291.         /// </summary>

292.         /// <returns></returns>

293.         private static Size GetMaximumScreenSizePrimary()

294.         {

295.             var scope = new System.Management.ManagementScope();

296.             var q = new System.Management.ObjectQuery("select * from CIM_VideoControllerResolution");

297.  

298.             UInt32 maxHResolution = 0;

299.             UInt32 maxVResolution = 0;

300.  

301.             using (var searcher = new System.Management.ManagementObjectSearcher(scope, q))

302.             {

303.                 var results = searcher.Get();

304.               

305.                 foreach (var item in results)

306.                 {

307.                     if ((UInt32)item["HorizontalResolution"] > maxHResolution)

308.                         maxHResolution = (UInt32)item["HorizontalResolution"];

309.  

310.                     if ((UInt32)item["VerticalResolution"] > maxVResolution)

311.                         maxVResolution = (UInt32)item["VerticalResolution"];

312.                 }

313.             }

314.             return new Size((int)maxHResolution, (int)maxVResolution);

315.         }

316.  

317.         /// <summary>

318.         /// 获取最大分辨率2

319.         /// CurrentHorizontalResolution:1920

320.         /// CurrentVerticalResolution:1080

321.         /// </summary>

322.         /// <returns></returns>

323.         public static Size GetGPUInfo2()

324.         {

325.             var gpu = new StringBuilder();

326.             var moc = new ManagementObjectSearcher("select * from Win32_VideoController").Get();

327.  

328.             var currentHorizontalResolution = 0;

329.             var currentVerticalResolution = 0;

330.  

331.             foreach (var mo in moc)

332.             {

333.                 foreach (var item in mo.Properties)

334.                 {

335.                     if (item.Name == "CurrentHorizontalResolution" && item.Value != null)

336.                         currentHorizontalResolution = int.Parse((item.Value.ToString()));

337.                     if (item.Name == "CurrentVerticalResolution" && item.Value != null)

338.                         currentVerticalResolution = int.Parse((item.Value.ToString()));

339.                     //gpu.Append($"{item.Name}:{item.Value}\r\n");

340.                 }

341.             }

342.             //var res = gpu.ToString();

343.             //return res;

344.             return new Size(currentHorizontalResolution, currentVerticalResolution);

345.         }

346.  

347.     }

348.  

349.     public class MonitorHelper

350.     {

351.         /// <summary>

352.         /// 获取DC句柄

353.         /// </summary>

354.         [DllImport("user32.dll")]

355.         static extern IntPtr GetDC(IntPtr hdc);

356.         /// <summary>

357.         /// 释放DC句柄

358.         /// </summary>

359.         [DllImport("user32.dll", EntryPoint = "ReleaseDC")]

360.         static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hdc);

361.         /// <summary>

362.         /// 获取句柄指定的数据

363.         /// </summary>

364.         [DllImport("gdi32.dll")]

365.         static extern int GetDeviceCaps(IntPtr hdc, int nIndex);

366.  

367.         /// <summary>

368.         /// 获取设置的分辨率(修改缩放,该值不改变)

369.         /// {Width = 1920 Height = 1080}

370.         /// </summary>

371.         /// <returns></returns>

372.         public static Size GetResolution()

373.         {

374.             Size size = new Size();

375.             IntPtr hdc = GetDC(IntPtr.Zero);

376.             size.Width = GetDeviceCaps(hdc, DeviceCapsType.DESKTOPHORZRES);

377.             size.Height = GetDeviceCaps(hdc, DeviceCapsType.DESKTOPVERTRES);

378.             ReleaseDC(IntPtr.Zero, hdc);

379.             return size;

380.         }

381.  

382.         /// <summary>

383.         /// 获取屏幕物理尺寸(mm,mm)

384.         /// {Width = 476 Height = 268}

385.         /// </summary>

386.         /// <returns></returns>

387.         public static Size GetScreenSize()

388.         {

389.             Size size = new Size();

390.             IntPtr hdc = GetDC(IntPtr.Zero);

391.             size.Width = GetDeviceCaps(hdc, DeviceCapsType.HORZSIZE);

392.             size.Height = GetDeviceCaps(hdc, DeviceCapsType.VERTSIZE);

393.             ReleaseDC(IntPtr.Zero, hdc);

394.             return size;

395.         }

396.  

397.         /// <summary>

398.         /// 获取屏幕的尺寸---inch

399.         /// 21.5

400.         /// </summary>

401.         /// <returns></returns>

402.         public static float GetScreenInch()

403.         {

404.             Size size = GetScreenSize();

405.             double inch = Math.Round(Math.Sqrt(Math.Pow(size.Width, 2) + Math.Pow(size.Height, 2)) / 25.4, 1);

406.             return (float)inch;

407.         }

408.     }

409.  

410.     /// <summary>

411.     /// GetDeviceCaps nidex

412.     /// </summary>

413.     public class DeviceCapsType

414.     {

415.         public const int DRIVERVERSION = 0;

416.         public const int TECHNOLOGY = 2;

417.         public const int HORZSIZE = 4;//以毫米为单位的显示宽度

418.         public const int VERTSIZE = 6;//以毫米为单位的显示高度

419.         public const int HORZRES = 8;

420.         public const int VERTRES = 10;

421.         public const int BITSPIXEL = 12;

422.         public const int PLANES = 14;

423.         public const int NUMBRUSHES = 16;

424.         public const int NUMPENS = 18;

425.         public const int NUMMARKERS = 20;

426.         public const int NUMFONTS = 22;

427.         public const int NUMCOLORS = 24;

428.         public const int PDEVICESIZE = 26;

429.         public const int CURVECAPS = 28;

430.         public const int LINECAPS = 30;

431.         public const int POLYGONALCAPS = 32;

432.         public const int TEXTCAPS = 34;

433.         public const int CLIPCAPS = 36;

434.         public const int RASTERCAPS = 38;

435.         public const int ASPECTX = 40;

436.         public const int ASPECTY = 42;

437.         public const int ASPECTXY = 44;

438.         public const int SHADEBLENDCAPS = 45;

439.         public const int LOGPIXELSX = 88;//像素/逻辑英寸(水平)

440.         public const int LOGPIXELSY = 90; //像素/逻辑英寸(垂直)

441.         public const int SIZEPALETTE = 104;

442.         public const int NUMRESERVED = 106;

443.         public const int COLORRES = 108;

444.         public const int PHYSICALWIDTH = 110;

445.         public const int PHYSICALHEIGHT = 111;

446.         public const int PHYSICALOFFSETX = 112;

447.         public const int PHYSICALOFFSETY = 113;

448.         public const int SCALINGFACTORX = 114;

449.         public const int SCALINGFACTORY = 115;

450.         public const int VREFRESH = 116;

451.         public const int DESKTOPVERTRES = 117;//垂直分辨率

452.         public const int DESKTOPHORZRES = 118;//水平分辨率

453.         public const int BLTALIGNMENT = 119;

454.     }

455.  

456.     /// <summary>

457.     /// 屏幕信息

458.     /// </summary>

459.     public class GPUInfo

460.     {

461.         /// <summary>

462.         /// 当前分辨率

463.         /// </summary>

464.         public Size CurrentResolution { get; set; }

465.         /// <summary>

466.         /// 最大分辨率

467.         /// </summary>

468.         public Size MaxScreenResolution { get; set; }

469.  

470.     }

471.  

472.     #endregion

473.  

474.     #region 硬盘信息

475.  

476.     public partial class ComputerInfo

477.     {

478.         /// <summary>

479.         /// 磁盘信息

480.         /// </summary>

481.         public static List<DiskInfo> GetDiskInfo()

482.         {

483.             List<DiskInfo> diskInfos = new List<DiskInfo>();

484.             try

485.             {

486.                 var moc = new ManagementClass("Win32_LogicalDisk").GetInstances();

487.                 foreach (ManagementObject mo in moc)

488.                 {

489.                     var diskInfo = new DiskInfo();

490.                     var diskInfoType = diskInfo.GetType();

491.                     var diskInfoFields = diskInfoType.GetProperties().ToList();

492.  

493.                     foreach (var item in mo.Properties)

494.                     {

495.                         if (diskInfoFields.Exists(f => f.Name == item.Name))

496.                         {

497.                             var p = diskInfoType.GetProperty(item.Name);

498.                             p.SetValue(diskInfo, item.Value);

499.                         }

500.                     }

501.                     diskInfos.Add(diskInfo);

502.                 }

503.  

504.                 //BGB

505.                 for (int i = 0; i < diskInfos.Count; i++)

506.                 {

507.                     diskInfos[i].Size = Math.Round((double)(diskInfos[i].Size / 1024.0 / 1024.0 / 1024.0), 1);

508.                     diskInfos[i].FreeSpace = Math.Round((double)(diskInfos[i].FreeSpace / 1024.0 / 1024.0 / 1024.0), 1);

509.                 }

510.             }

511.             catch (Exception ex)

512.             {

513.  

514.             }

515.  

516.             return diskInfos;

517.         }

518.     }

519.  

520.     /// <summary>

521.     /// 磁盘信息

522.     /// </summary>

523.     public class DiskInfo

524.     {

525.         /// <summary>

526.         /// 磁盘ID 如:D:

527.         /// </summary>

528.         public string DeviceID { get; set; }

529.         /// <summary>

530.         /// 驱动器类型。3为本地固定磁盘,2为可移动磁盘

531.         /// </summary>

532.         public uint DriveType { get; set; }

533.         /// <summary>

534.         /// 磁盘名称。如:系统

535.         /// </summary>

536.         public string VolumeName { get; set; }

537.         /// <summary>

538.         /// 描述。如:本地固定磁盘

539.         /// </summary>

540.         public string Description { get; set; }

541.         /// <summary>

542.         /// 文件系统。如:NTFS

543.         /// </summary>

544.         public string FileSystem { get; set; }

545.         /// <summary>

546.         /// 磁盘容量(GB

547.         /// </summary>

548.         public double Size { get; set; }

549.         /// <summary>

550.         /// 可用空间(GB

551.         /// </summary>

552.         public double FreeSpace { get; set; }

553.  

554.         public override string ToString()

555.         {

556.             return $"{VolumeName}({DeviceID}), {FreeSpace}GB is available for {Size}GB in total, {FileSystem}, {Description}";

557.         }

558.  

559.     }

560.  

561.     #endregion

可以获取下面这些信息:

ComputerCheck Info:
System Info:Windows 10 Enterprise, Enterprise, X64, Microsoft Windows 10.0.18363 
CPU Info:Intel(R) Core(TM) i3-8100 CPU @ 3.60GHz, 4 Core 4 Threads
RAM Info:12/15.8GB(75%)
GPU Info:1920*1080 / 1920*1080
Disk Info:系统(C:), 74.2GB is available for 238.1GB in total, NTFS, 本地固定磁盘
软件(D:), 151.9GB is available for 300GB in total, NTFS, 本地固定磁盘
办公(E:), 30.7GB is available for 300GB in total, NTFS, 本地固定磁盘
其它(F:), 256.3GB is available for 331.5GB in total, NTFS, 本地固定磁盘


该文章在 2024/11/20 9:47:39 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved