Created
June 15, 2020 15:31
-
-
Save poychang/b9a9dc756867c9d34ec07142bce876c4 to your computer and use it in GitHub Desktop.
[WMI Searcher] 返回容易存取的物件
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
void Main() | |
{ | |
GetComponent("Win32_PhysicalMemory") | |
.Select(p => p.ToObject<Win32_PhysicalMemory>()) | |
.ToList() | |
.ForEach(item => Console.WriteLine(item.Manufacturer)); | |
} | |
/// <summary> | |
/// 取得指定硬體設備類別的所有屬性資料 | |
/// </summary> | |
/// <param name="hardwareClass">WMI 的硬體設備類別名稱</param> | |
/// <returns></returns> | |
IEnumerable<IDictionary<string, object>> GetComponent(string hardwareClass) | |
{ | |
var managementObjectSearcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM " + hardwareClass); | |
foreach (var managementObject in managementObjectSearcher.Get()) | |
{ | |
var dynamicObject = new ExpandoObject() as IDictionary<string, object>; | |
foreach (var prop in managementObject.Properties) | |
{ | |
dynamicObject.Add(prop.Name, prop.Value); | |
} | |
yield return dynamicObject; | |
} | |
} | |
/// <summary> | |
/// 記憶體硬體設備的 Win32 屬性 | |
/// </summary> | |
/// <remarks>https://docs.microsoft.com/zh-tw/windows/win32/cimwin32prov/win32-physicalmemory</remarks> | |
public class Win32_PhysicalMemory | |
{ | |
public UInt32 Attributes { get; set; } | |
public string BankLabel { get; set; } | |
public UInt64 Capacity { get; set; } | |
public string Caption { get; set; } | |
public UInt32 ConfiguredClockSpeed { get; set; } | |
public UInt32 ConfiguredVoltage { get; set; } | |
public string CreationClassName { get; set; } | |
public UInt16 DataWidth { get; set; } | |
public string Description { get; set; } | |
public string DeviceLocator { get; set; } | |
public UInt16 FormFactor { get; set; } | |
public bool HotSwappable { get; set; } | |
public DateTime InstallDate { get; set; } | |
public UInt16 InterleaveDataDepth { get; set; } | |
public UInt32 InterleavePosition { get; set; } | |
public string Manufacturer { get; set; } | |
public UInt32 MaxVoltage { get; set; } | |
public UInt16 MemoryType { get; set; } | |
public UInt32 MinVoltage { get; set; } | |
public string Model { get; set; } | |
public string Name { get; set; } | |
public string OtherIdentifyingInfo { get; set; } | |
public string PartNumber { get; set; } | |
public UInt32 PositionInRow { get; set; } | |
public bool PoweredOn { get; set; } | |
public bool Removable { get; set; } | |
public bool Replaceable { get; set; } | |
public string SerialNumber { get; set; } | |
public string SKU { get; set; } | |
public UInt32 SMBIOSMemoryType { get; set; } | |
public UInt32 Speed { get; set; } | |
public string Status { get; set; } | |
public string Tag { get; set; } | |
public UInt16 TotalWidth { get; set; } | |
public UInt16 TypeDetail { get; set; } | |
public string Version { get; set; } | |
}; | |
public static class MyExtensions | |
{ | |
/// <summary> | |
/// 轉型成指定的類別 | |
/// </summary> | |
public static T ToObject<T>(this IDictionary<string, object> dict) | |
{ | |
var type = typeof(T); | |
var obj = Activator.CreateInstance(type); | |
foreach (var kv in dict) | |
{ | |
type.GetProperty(kv.Key).SetValue(obj, kv.Value); | |
} | |
return (T)obj; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment