Created
December 19, 2016 13:27
-
-
Save V0v1kkk/d14c67f9c970c1097c06aee0bd73a2f8 to your computer and use it in GitHub Desktop.
C# Get hardware information whith WMI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Use WMI for check percentage of CPU Usage | |
/// Write to private field _cpuUsage | |
/// </summary> | |
private void CheckCpuUsage() | |
{ | |
try | |
{ | |
var searcher = | |
new ManagementObjectSearcher("select * from Win32_PerfFormattedData_PerfOS_Processor"); | |
var cpuTimes = searcher.Get() | |
.Cast<ManagementObject>() | |
.Select(mo => new | |
{ | |
Name = mo["Name"], | |
Usage = mo["PercentProcessorTime"] | |
} | |
).ToArray(); | |
_cpuUsage = (ulong) cpuTimes[0].Usage; //atomic assignment | |
} | |
catch (Exception exception) | |
{ | |
CheckCpuUsageErrors++; | |
_subscriberOnMessages($"Error on checking cpu usage: {exception.Message}"); | |
_subscriberOnMessages($"Total errors on checking cpu usage: {CheckCpuUsageErrors}"); | |
} | |
} | |
/// <summary> | |
/// Use WMI for check RAM usage (int %) | |
/// Write to private field _memoryUsage | |
/// </summary> | |
private void CheckMemoryUsage() | |
{ | |
try | |
{ | |
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem"); | |
var hddUsage = searcher.Get().Cast<ManagementObject>().Select(m=>new | |
{ | |
TotalVisibleMemorySize = m["TotalVisibleMemorySize"], | |
FreeMemory = m["FreePhysicalMemory"] | |
}).FirstOrDefault(); | |
if(hddUsage == null) throw new Exception("Can't recive informacion about memory usage."); | |
var freeRam = (ulong) hddUsage.FreeMemory; | |
var totalRam = (ulong) hddUsage.TotalVisibleMemorySize; | |
_memoryUsage = (totalRam - freeRam)/(float)totalRam * 100; | |
} | |
catch (Exception exception) | |
{ | |
CheckMemoryUsageErrors++; | |
_subscriberOnMessages($"Error on checking RAM usage: {exception.Message}"); | |
_subscriberOnMessages($"Total errors on checking RAM usage: {CheckMemoryUsageErrors}"); | |
} | |
} | |
/// <summary> | |
/// Use WMI for check HddUsage (PercentDiskTime) | |
/// Write to private field _hddUsage | |
/// </summary> | |
/// <param name="diskLetter">Disk name in format: "D:\\"</param> | |
private void CheckHddUsage(string diskLetter) | |
{ | |
diskLetter = diskLetter.Replace("\\", ""); | |
try | |
{ | |
var searcher = | |
new ManagementObjectSearcher("select * from Win32_PerfFormattedData_PerfDisk_PhysicalDisk"); | |
var hddUsage = searcher.Get() | |
.Cast<ManagementObject>() | |
.Select(mo => new | |
{ | |
Usage = mo["PercentDiskTime"], | |
Name = mo["Name"] | |
}).ToArray(); | |
for (var i = 0; i < hddUsage.Length; i++) | |
{ | |
if (!hddUsage[i].Name.ToString().Contains(diskLetter)) continue; | |
_hddUsage = (ulong) hddUsage[i].Usage; //atomic assignment | |
break; | |
} | |
} | |
catch (Exception exception) | |
{ | |
CheckHddUsageErrors++; | |
_subscriberOnMessages($"Error on checking hdd usage: {exception.Message}"); | |
_subscriberOnMessages($"Total errors on checking hdd usage: {CheckHddUsageErrors}"); | |
} | |
} | |
/// <summary> | |
/// Use WMI for check amount of free memory | |
/// </summary> | |
/// <returns>Amount of free memory in bytes</returns> | |
private ulong GetFreeMomory() | |
{ | |
try | |
{ | |
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem"); | |
return searcher.Get().Cast<ManagementObject>().Select(m => (ulong)m["FreePhysicalMemory"]).FirstOrDefault() * 1024; | |
} | |
catch (Exception exception) | |
{ | |
_subscriberOnMessages($"Error on checking free RAM: {exception.Message}"); | |
_subscriberOnMessages(exception.StackTrace); | |
return 0; | |
} | |
} | |
/// <summary> | |
/// Use WMI for check type of disk | |
/// </summary> | |
/// <returns>One of DiskTypeEnum: Hdd or Ssd</returns> | |
private DiskTypeEnum GetDriveType(string diskName) | |
{ | |
try | |
{ | |
char driveLetter = diskName[0]; | |
if(driveLetter < 'A' || driveLetter > 'Z') throw new Exception("Failed to recognize drive character."); | |
using (var partitionSearcher = new ManagementObjectSearcher( | |
@"\\localhost\ROOT\Microsoft\Windows\Storage", "SELECT * FROM MSFT_Partition")) | |
{ | |
var partitions = partitionSearcher.Get().Cast<ManagementBaseObject>(); | |
ManagementBaseObject partition = partitions.FirstOrDefault(managementBaseObject => (char) managementBaseObject["DriveLetter"] == driveLetter); | |
if(partition == null) throw new Exception("Filed to recognize disk partition containing file."); | |
using (var physicalDiskSearcher = new ManagementObjectSearcher( | |
@"\\localhost\ROOT\Microsoft\Windows\Storage", "SELECT * FROM MSFT_PhysicalDisk")) | |
{ | |
var physicalDisk = physicalDiskSearcher.Get().Cast<ManagementBaseObject>().FirstOrDefault(m=> ((string)m["DeviceID"]).Contains(((uint)partition["DiskNumber"]).ToString())); | |
if (physicalDisk == null) throw new Exception("Error on WMI request of disk type"); | |
if (((ushort) physicalDisk["MediaType"] == 4) && ((uint) physicalDisk["SpindleSpeed"] == 0)) | |
{ | |
return DiskTypeEnum.Ssd; | |
} | |
return DiskTypeEnum.Hdd; | |
} | |
} | |
} | |
catch (Exception exception) | |
{ | |
_subscriberOnMessages($"Error on getting disk type: {exception.Message}"); | |
_subscriberOnMessages(exception.StackTrace); | |
return DiskTypeEnum.Hdd; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The title is wrong written:
"C# Get hardware information whith WMI" needs to be corrected to "C# Get hardware information with WMI".
("with", not "whith"!)