IndiciumHelper.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Management;
  5. namespace Optimizer
  6. {
  7. public static class IndiciumHelper
  8. {
  9. public static List<Volume> Volumes = new List<Volume>();
  10. public static List<Volume> Opticals = new List<Volume>();
  11. public static List<Volume> Removables = new List<Volume>();
  12. public static List<NetworkDevice> PhysicalAdapters = new List<NetworkDevice>();
  13. public static List<NetworkDevice> VirtualAdapters = new List<NetworkDevice>();
  14. public static List<Keyboard> Keyboards = new List<Keyboard>();
  15. public static List<PointingDevice> PointingDevices = new List<PointingDevice>();
  16. public static List<CPU> GetCPUs()
  17. {
  18. List<CPU> CPUs = new List<CPU>();
  19. CPU cpu;
  20. ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
  21. foreach (ManagementObject mo in searcher.Get())
  22. {
  23. cpu = new CPU();
  24. cpu.Name = Convert.ToString(mo.Properties["Name"].Value);
  25. cpu.L2CacheSize = ByteSize.FromKiloBytes(Convert.ToDouble(mo.Properties["L2CacheSize"].Value));
  26. cpu.L3CacheSize = ByteSize.FromKiloBytes(Convert.ToDouble(mo.Properties["L3CacheSize"].Value));
  27. cpu.Cores = Convert.ToUInt32(mo.Properties["NumberOfCores"].Value);
  28. cpu.Threads = Convert.ToUInt32(mo.Properties["ThreadCount"].Value);
  29. cpu.LogicalCpus = Convert.ToUInt32(mo.Properties["NumberOfLogicalProcessors"].Value);
  30. bool temp = Convert.ToBoolean(mo.Properties["VirtualizationFirmwareEnabled"].Value);
  31. cpu.Virtualization = (temp) ? "Yes" : "No";
  32. cpu.Stepping = Convert.ToString(mo.Properties["Description"].Value);
  33. cpu.Revision = Convert.ToString(mo.Properties["Revision"].Value);
  34. try
  35. {
  36. ManagementObjectSearcher searcher2 = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
  37. foreach (ManagementObject mo2 in searcher2.Get())
  38. {
  39. bool temp2 = Convert.ToBoolean(mo2.Properties["DataExecutionPrevention_Available"].Value);
  40. cpu.DataExecutionPrevention = (temp2) ? "Yes" : "No";
  41. }
  42. }
  43. catch { }
  44. if (string.IsNullOrEmpty(cpu.Name)) cpu.Name = GetCPUNameAlternative();
  45. CPUs.Add(cpu);
  46. }
  47. return CPUs;
  48. }
  49. private static string GetCPUNameAlternative()
  50. {
  51. using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DESCRIPTION\System\CentralProcessor\0", false))
  52. {
  53. return key.GetValue("ProcessorNameString").ToString();
  54. }
  55. }
  56. public static VirtualMemory GetVM()
  57. {
  58. VirtualMemory vm = new VirtualMemory();
  59. ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
  60. foreach (ManagementObject mo in searcher.Get())
  61. {
  62. vm.TotalVirtualMemory = ByteSize.FromKiloBytes(Convert.ToUInt64(mo.Properties["TotalVirtualMemorySize"].Value));
  63. vm.AvailableVirtualMemory = ByteSize.FromKiloBytes(Convert.ToUInt64(mo.Properties["FreeVirtualMemory"].Value));
  64. vm.UsedVirtualMemory = vm.TotalVirtualMemory.Subtract(vm.AvailableVirtualMemory);
  65. }
  66. return vm;
  67. }
  68. public static List<RAM> GetRAM()
  69. {
  70. List<RAM> modules = new List<RAM>();
  71. RAM module;
  72. ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMemory");
  73. foreach (ManagementObject mo in searcher.Get())
  74. {
  75. module = new RAM();
  76. module.BankLabel = Convert.ToString(mo.Properties["BankLabel"].Value);
  77. module.Capacity = ByteSize.FromBytes(Convert.ToDouble(mo.Properties["Capacity"].Value));
  78. module.Manufacturer = Convert.ToString(mo.Properties["Manufacturer"].Value);
  79. module.Speed = Convert.ToUInt32(mo.Properties["Speed"].Value);
  80. UInt16 memorytype = Convert.ToUInt16(mo.Properties["MemoryType"].Value);
  81. UInt16 formfactor = Convert.ToUInt16(mo.Properties["FormFactor"].Value);
  82. module.MemoryType = SanitizeMemoryType(memorytype);
  83. module.FormFactor = SanitizeFormFactor(formfactor);
  84. modules.Add(module);
  85. }
  86. return modules;
  87. }
  88. public static List<Motherboard> GetMotherboards()
  89. {
  90. List<Motherboard> mobos = new List<Motherboard>();
  91. ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");
  92. foreach (ManagementObject mo in searcher.Get())
  93. {
  94. Motherboard mobo = new Motherboard();
  95. mobo.Model = Convert.ToString(mo.Properties["Model"].Value);
  96. mobo.Manufacturer = Convert.ToString(mo.Properties["Manufacturer"].Value);
  97. mobo.Product = Convert.ToString(mo.Properties["Product"].Value);
  98. mobo.Version = Convert.ToString(mo.Properties["Version"].Value);
  99. try
  100. {
  101. ManagementObjectSearcher searcher2 = new ManagementObjectSearcher("SELECT * FROM Win32_IDEController");
  102. foreach (ManagementObject mo2 in searcher2.Get())
  103. {
  104. mobo.Chipset = Convert.ToString(mo2.Properties["Description"].Value);
  105. }
  106. }
  107. catch { }
  108. try
  109. {
  110. ManagementObjectSearcher searcher3 = new ManagementObjectSearcher("SELECT * FROM Win32_IDEController");
  111. foreach (ManagementObject mo3 in searcher3.Get())
  112. {
  113. mobo.Revision = Convert.ToString(mo3.Properties["RevisionNumber"].Value);
  114. }
  115. }
  116. catch { }
  117. try
  118. {
  119. ManagementObjectSearcher searcher4 = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem");
  120. foreach (ManagementObject mo4 in searcher4.Get())
  121. {
  122. mobo.SystemModel = Convert.ToString(mo4.Properties["Model"].Value);
  123. }
  124. }
  125. catch { }
  126. ManagementObjectSearcher searcher5 = new ManagementObjectSearcher("SELECT * FROM Win32_BIOS");
  127. foreach (ManagementObject mo5 in searcher5.Get())
  128. {
  129. mobo.BIOSName = Convert.ToString(mo5.Properties["Name"].Value);
  130. mobo.BIOSManufacturer = Convert.ToString(mo5.Properties["Manufacturer"].Value);
  131. mobo.BIOSVersion = Convert.ToString(mo5.Properties["Version"].Value);
  132. mobo.BIOSBuildNumber = Convert.ToString(mo5.Properties["BuildNumber"].Value);
  133. //ReleaseDate = DateTime.Parse(mo.Properties["ReleaseDate"].Value.ToString());
  134. }
  135. mobos.Add(mobo);
  136. }
  137. return mobos;
  138. }
  139. public static List<Disk> GetDisks()
  140. {
  141. List<Disk> disks = new List<Disk>();
  142. ManagementObjectSearcher searcher2 = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
  143. foreach (ManagementObject mo2 in searcher2.Get())
  144. {
  145. Disk disk = new Disk();
  146. disk.Model = Convert.ToString(mo2.Properties["Model"].Value);
  147. disk.BytesPerSector = Convert.ToUInt32(mo2.Properties["BytesPerSector"].Value);
  148. disk.FirmwareRevision = Convert.ToString(mo2.Properties["FirmwareRevision"].Value);
  149. disk.MediaType = Convert.ToString(mo2.Properties["MediaType"].Value);
  150. disk.Capacity = ByteSize.FromBytes(Convert.ToDouble(mo2.Properties["Size"].Value));
  151. disks.Add(disk);
  152. }
  153. return disks;
  154. }
  155. public static void GetVolumes()
  156. {
  157. ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Volume");
  158. foreach (ManagementObject mo in searcher.Get())
  159. {
  160. Volume volume = new Volume();
  161. volume.BlockSize = Convert.ToUInt64(mo.Properties["BlockSize"].Value);
  162. volume.Capacity = ByteSize.FromBytes(Convert.ToDouble(mo.Properties["Capacity"].Value));
  163. bool temp = Convert.ToBoolean(mo.Properties["Compressed"].Value);
  164. volume.Compressed = (temp) ? "Yes" : "No";
  165. volume.DriveLetter = Convert.ToString(mo.Properties["DriveLetter"].Value);
  166. UInt32 i = Convert.ToUInt32(mo.Properties["DriveType"].Value);
  167. volume.DriveType = SanitizeDriveType(i);
  168. volume.FileSystem = Convert.ToString(mo.Properties["FileSystem"].Value);
  169. volume.FreeSpace = ByteSize.FromBytes(Convert.ToDouble(mo.Properties["FreeSpace"].Value));
  170. volume.UsedSpace = volume.Capacity.Subtract(volume.FreeSpace);
  171. bool temp2 = Convert.ToBoolean(mo.Properties["IndexingEnabled"].Value);
  172. volume.Indexing = (temp2) ? "Yes" : "No";
  173. volume.Label = Convert.ToString(mo.Properties["Label"].Value);
  174. if (i == 2)
  175. {
  176. Removables.Add(volume);
  177. }
  178. else if (i == 5)
  179. {
  180. Opticals.Add(volume);
  181. }
  182. else
  183. {
  184. Volumes.Add(volume);
  185. }
  186. }
  187. }
  188. public static void GetPeripherals()
  189. {
  190. ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Keyboard");
  191. foreach (ManagementObject mo in searcher.Get())
  192. {
  193. Keyboard keyboard = new Keyboard();
  194. keyboard.Name = Convert.ToString(mo.Properties["Description"].Value);
  195. keyboard.Layout = Convert.ToString(mo.Properties["Layout"].Value);
  196. keyboard.Status = Convert.ToString(mo.Properties["Status"].Value);
  197. keyboard.FunctionKeys = Convert.ToUInt16(mo.Properties["NumberOfFunctionKeys"].Value);
  198. bool temp = Convert.ToBoolean(mo.Properties["IsLocked"].Value);
  199. keyboard.Locked = (temp) ? "Yes" : "No";
  200. Keyboards.Add(keyboard);
  201. }
  202. ManagementObjectSearcher searcher2 = new ManagementObjectSearcher("SELECT * FROM Win32_PointingDevice");
  203. foreach (ManagementObject mo2 in searcher2.Get())
  204. {
  205. PointingDevice pointingDevice = new PointingDevice();
  206. pointingDevice.Name = Convert.ToString(mo2.Properties["Description"].Value);
  207. pointingDevice.Manufacturer = Convert.ToString(mo2.Properties["Manufacturer"].Value);
  208. pointingDevice.Status = Convert.ToString(mo2.Properties["Status"].Value);
  209. pointingDevice.Buttons = Convert.ToUInt16(mo2.Properties["NumberOfButtons"].Value);
  210. bool temp = Convert.ToBoolean(mo2.Properties["IsLocked"].Value);
  211. pointingDevice.Locked = (temp) ? "Yes" : "No";
  212. pointingDevice.HardwareType = Convert.ToString(mo2.Properties["HardwareType"].Value);
  213. UInt16 i = Convert.ToUInt16(mo2.Properties["PointingType"].Value);
  214. pointingDevice.PointingType = SanitizePointingType(i);
  215. UInt16 i2 = Convert.ToUInt16(mo2.Properties["DeviceInterface"].Value);
  216. pointingDevice.DeviceInterface = SanitizeDeviceInterface(i2);
  217. PointingDevices.Add(pointingDevice);
  218. }
  219. }
  220. public static List<GPU> GetGPUs()
  221. {
  222. List<GPU> GPUs = new List<GPU>();
  223. ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_VideoController");
  224. foreach (ManagementObject mo in searcher.Get())
  225. {
  226. GPU gpu = new GPU();
  227. gpu.Name = Convert.ToString(mo.Properties["Name"].Value);
  228. gpu.Memory = ByteSize.FromBytes(Convert.ToDouble(mo.Properties["AdapterRAM"].Value));
  229. gpu.ResolutionX = Convert.ToUInt32(mo.Properties["CurrentHorizontalResolution"].Value);
  230. gpu.ResolutionY = Convert.ToUInt32(mo.Properties["CurrentVerticalResolution"].Value);
  231. gpu.RefreshRate = Convert.ToUInt32(mo.Properties["CurrentRefreshRate"].Value);
  232. gpu.DACType = Convert.ToString(mo.Properties["AdapterDACType"].Value);
  233. UInt16 vtype = Convert.ToUInt16(mo.Properties["VideoMemoryType"].Value);
  234. gpu.VideoMemoryType = SanitizeVideoMemoryType(vtype);
  235. GPUs.Add(gpu);
  236. }
  237. return GPUs;
  238. }
  239. public static void GetNetworkAdapters()
  240. {
  241. ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter");
  242. foreach (ManagementObject mo in searcher.Get())
  243. {
  244. NetworkDevice adapter = new NetworkDevice();
  245. adapter.AdapterType = Convert.ToString(mo.Properties["AdapterType"].Value);
  246. adapter.Manufacturer = Convert.ToString(mo.Properties["Manufacturer"].Value);
  247. adapter.ProductName = Convert.ToString(mo.Properties["ProductName"].Value);
  248. bool temp = Convert.ToBoolean(mo.Properties["PhysicalAdapter"].Value);
  249. adapter.PhysicalAdapter = (temp) ? "Yes" : "No";
  250. adapter.MacAddress = Convert.ToString(mo.Properties["MacAddress"].Value);
  251. adapter.ServiceName = Convert.ToString(mo.Properties["ServiceName"].Value);
  252. if (temp)
  253. {
  254. PhysicalAdapters.Add(adapter);
  255. }
  256. else
  257. {
  258. VirtualAdapters.Add(adapter);
  259. }
  260. }
  261. }
  262. public static List<AudioDevice> GetAudioDevices()
  263. {
  264. List<AudioDevice> audioDevices = new List<AudioDevice>();
  265. ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_SoundDevice");
  266. foreach (ManagementObject mo in searcher.Get())
  267. {
  268. AudioDevice device = new AudioDevice();
  269. device.ProductName = Convert.ToString(mo.Properties["ProductName"].Value);
  270. device.Manufacturer = Convert.ToString(mo.Properties["Manufacturer"].Value);
  271. device.Status = Convert.ToString(mo.Properties["Status"].Value);
  272. audioDevices.Add(device);
  273. }
  274. return audioDevices;
  275. }
  276. private static string SanitizeDriveType(UInt32 i)
  277. {
  278. string result = string.Empty;
  279. switch (i)
  280. {
  281. case 0:
  282. result = "Unknown";
  283. break;
  284. case 1:
  285. result = "No Root Directory";
  286. break;
  287. case 2:
  288. result = "Removable Disk";
  289. break;
  290. case 3:
  291. result = "Local Disk";
  292. break;
  293. case 4:
  294. result = "Network Drive";
  295. break;
  296. case 5:
  297. result = "Compact Disk";
  298. break;
  299. case 6:
  300. result = "RAM Disk";
  301. break;
  302. }
  303. return result;
  304. }
  305. private static string SanitizeVideoMemoryType(UInt16 i)
  306. {
  307. string result = string.Empty;
  308. switch (i)
  309. {
  310. case 1:
  311. result = "Other";
  312. break;
  313. case 2:
  314. result = "Unknown";
  315. break;
  316. case 3:
  317. result = "VRAM";
  318. break;
  319. case 4:
  320. result = "DRAM";
  321. break;
  322. case 5:
  323. result = "SRAM";
  324. break;
  325. case 6:
  326. result = "WRAM";
  327. break;
  328. case 7:
  329. result = "EDO RAM";
  330. break;
  331. case 8:
  332. result = "Burst Synchronous DRAM";
  333. break;
  334. case 9:
  335. result = "Pipelined Burst SRAM";
  336. break;
  337. case 10:
  338. result = "CDRAM";
  339. break;
  340. case 11:
  341. result = "3DRAM";
  342. break;
  343. case 12:
  344. result = "SDRAM";
  345. break;
  346. case 13:
  347. result = "SGRAM";
  348. break;
  349. }
  350. return result;
  351. }
  352. private static string SanitizeFormFactor(UInt16 i)
  353. {
  354. string result = string.Empty;
  355. switch (i)
  356. {
  357. case 0:
  358. result = "Unknown";
  359. break;
  360. case 1:
  361. result = "Other";
  362. break;
  363. case 2:
  364. result = "SIP";
  365. break;
  366. case 3:
  367. result = "DIP";
  368. break;
  369. case 4:
  370. result = "ZIP";
  371. break;
  372. case 5:
  373. result = "SOJ";
  374. break;
  375. case 6:
  376. result = "Proprietary";
  377. break;
  378. case 7:
  379. result = "SIMM";
  380. break;
  381. case 8:
  382. result = "DIMM";
  383. break;
  384. case 9:
  385. result = "TSOP";
  386. break;
  387. case 10:
  388. result = "PGA";
  389. break;
  390. case 11:
  391. result = "RIMM";
  392. break;
  393. case 12:
  394. result = "SODIMM";
  395. break;
  396. case 13:
  397. result = "SRIMM";
  398. break;
  399. case 14:
  400. result = "SMD";
  401. break;
  402. case 15:
  403. result = "SSMP";
  404. break;
  405. case 16:
  406. result = "QFP";
  407. break;
  408. case 17:
  409. result = "TQFP";
  410. break;
  411. case 18:
  412. result = "SOIC";
  413. break;
  414. case 19:
  415. result = "LCC";
  416. break;
  417. case 20:
  418. result = "PLCC";
  419. break;
  420. case 21:
  421. result = "BGA";
  422. break;
  423. case 22:
  424. result = "FPBGA";
  425. break;
  426. case 23:
  427. result = "LGA";
  428. break;
  429. }
  430. return result;
  431. }
  432. private static string SanitizeMemoryType(UInt16 i)
  433. {
  434. string result = string.Empty;
  435. switch (i)
  436. {
  437. case 0:
  438. result = "Unknown";
  439. break;
  440. case 1:
  441. result = "Other";
  442. break;
  443. case 2:
  444. result = "DRAM";
  445. break;
  446. case 3:
  447. result = "Synchonous DRAM";
  448. break;
  449. case 4:
  450. result = "Cache DRAM";
  451. break;
  452. case 5:
  453. result = "EDO";
  454. break;
  455. case 6:
  456. result = "EDRAM";
  457. break;
  458. case 7:
  459. result = "VRAM";
  460. break;
  461. case 8:
  462. result = "SRAM";
  463. break;
  464. case 9:
  465. result = "RAM";
  466. break;
  467. case 10:
  468. result = "ROM";
  469. break;
  470. case 11:
  471. result = "Flash";
  472. break;
  473. case 12:
  474. result = "EEPROM";
  475. break;
  476. case 13:
  477. result = "FEPROM";
  478. break;
  479. case 14:
  480. result = "EPROM";
  481. break;
  482. case 15:
  483. result = "CDRAM";
  484. break;
  485. case 16:
  486. result = "3DRAM";
  487. break;
  488. case 17:
  489. result = "SDRAM";
  490. break;
  491. case 18:
  492. result = "SGRAM";
  493. break;
  494. case 19:
  495. result = "RDRAM";
  496. break;
  497. case 20:
  498. result = "DDR";
  499. break;
  500. case 21:
  501. result = "DDR2";
  502. break;
  503. case 22:
  504. result = "DDR2 FB-DIMM";
  505. break;
  506. case 24:
  507. result = "DDR3";
  508. break;
  509. case 25:
  510. result = "FBD2";
  511. break;
  512. }
  513. return result;
  514. }
  515. private static string SanitizeDeviceInterface(UInt16 i)
  516. {
  517. string result = string.Empty;
  518. switch (i)
  519. {
  520. case 1:
  521. result = "Other";
  522. break;
  523. case 2:
  524. result = "Unknown";
  525. break;
  526. case 3:
  527. result = "Serial";
  528. break;
  529. case 4:
  530. result = "PS/2";
  531. break;
  532. case 5:
  533. result = "Infrared";
  534. break;
  535. case 6:
  536. result = "HP-HIL";
  537. break;
  538. case 7:
  539. result = "Bus mouse";
  540. break;
  541. case 8:
  542. result = "ADB (Apple Desktop Bus)";
  543. break;
  544. case 160:
  545. result = "Bus mouse DB-9";
  546. break;
  547. case 161:
  548. result = "Bus mouse micro-DIN";
  549. break;
  550. case 162:
  551. result = "USB";
  552. break;
  553. }
  554. return result;
  555. }
  556. private static string SanitizePointingType(UInt16 i)
  557. {
  558. string result = string.Empty;
  559. switch (i)
  560. {
  561. case 1:
  562. result = "Other";
  563. break;
  564. case 2:
  565. result = "Unknown";
  566. break;
  567. case 3:
  568. result = "Mouse";
  569. break;
  570. case 4:
  571. result = "Track Ball";
  572. break;
  573. case 5:
  574. result = "Track Point";
  575. break;
  576. case 6:
  577. result = "Glide Point";
  578. break;
  579. case 7:
  580. result = "Touch Pad";
  581. break;
  582. case 8:
  583. result = "Touch Screen";
  584. break;
  585. case 9:
  586. result = "Mouse - Optical Sensor";
  587. break;
  588. }
  589. return result;
  590. }
  591. }
  592. }