如何在 C# 中检索硬件信息?
How to retrieve hardware info in c#?
我想将命令 'wmic bios get' 的结果导出到 xml 文件。如何读取这些数据并将每个数据添加为 xml 节点?
前段时间实际上必须做的事情
首先你需要 class 来使用 wmi 处理请求:
public class GetHW
{
public string identifier(string wmiClass, string wmiProperty, string wmiMustBeTrue)
{
string result = "";
System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
System.Management.ManagementObjectCollection moc = mc.GetInstances();
foreach (System.Management.ManagementObject mo in moc)
{
if (mo[wmiMustBeTrue].ToString() == "True")
{
//Only get the first one
if (result == "")
{
try
{
result = mo[wmiProperty].ToString();
break;
}
catch
{
}
}
}
}
return result;
}
public string identifier(string wmiClass, string wmiProperty)
{
string result = "";
System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
System.Management.ManagementObjectCollection moc = mc.GetInstances();
foreach (System.Management.ManagementObject mo in moc)
{
//Only get the first one
if (result == "")
{
try
{
result = mo[wmiProperty].ToString();
break;
}
catch
{
}
}
}
return result;
}
}
然后在您的实际应用程序查询中 class 像这样:
GetHW HW = new GetHW();
Dictionary <string, string> data = new Dictionary<string,string();
data.Add("Manufacturer", HW.identifier("Win32_BIOS", "Manufacturer"));
data.Add("SMBIOSBIOSVersion", HW.identifier("Win32_BIOS", "SMBIOSBIOSVersion"));
data.Add("IdentificationCode", HW.identifier("Win32_BIOS", "IdentificationCode"));
data.Add("SerialNumber", HW.identifier("Win32_BIOS", "SerialNumber"));
data.Add("ReleaseDate", HW.identifier("Win32_BIOS", "ReleaseDate"));
data.Add("Version", HW.identifier("Win32_BIOS", "Version"));
然后用字典做任何你想做的事。
对于 bios 数据来说,这可能有点矫枉过正,但如果您需要,它可以让您提取其他数据——例如主板:
data.Add("Model", HW.identifier("Win32_BaseBoard", "Model"));
data.Add("Manufacturer", HW.identifier("Win32_BaseBoard", "Manufacturer"));
data.Add("Name", HW.identifier("Win32_BaseBoard", "Name"));
data.Add("SerialNumber", HW.identifier("Win32_BaseBoard", "SerialNumber"));
或CPU
data.Add("Unique ID", HW.identifier("Win32_Processor", "UniqueId"));
data.Add("ID", HW.identifier("Win32_Processor", "ProcessorId"));
data.Add("Name", HW.identifier("Win32_Processor", "Name"));
data.Add("Manufacturer", HW.identifier("Win32_Processor", "Manufacturer"));
data.Add("MaxClockSpeed", HW.identifier("Win32_Processor", "MaxClockSpeed"));
编辑:以防万一有人需要 - here 是所有 win32 classes 的列表。
我想将命令 'wmic bios get' 的结果导出到 xml 文件。如何读取这些数据并将每个数据添加为 xml 节点?
前段时间实际上必须做的事情 首先你需要 class 来使用 wmi 处理请求:
public class GetHW
{
public string identifier(string wmiClass, string wmiProperty, string wmiMustBeTrue)
{
string result = "";
System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
System.Management.ManagementObjectCollection moc = mc.GetInstances();
foreach (System.Management.ManagementObject mo in moc)
{
if (mo[wmiMustBeTrue].ToString() == "True")
{
//Only get the first one
if (result == "")
{
try
{
result = mo[wmiProperty].ToString();
break;
}
catch
{
}
}
}
}
return result;
}
public string identifier(string wmiClass, string wmiProperty)
{
string result = "";
System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
System.Management.ManagementObjectCollection moc = mc.GetInstances();
foreach (System.Management.ManagementObject mo in moc)
{
//Only get the first one
if (result == "")
{
try
{
result = mo[wmiProperty].ToString();
break;
}
catch
{
}
}
}
return result;
}
}
然后在您的实际应用程序查询中 class 像这样:
GetHW HW = new GetHW();
Dictionary <string, string> data = new Dictionary<string,string();
data.Add("Manufacturer", HW.identifier("Win32_BIOS", "Manufacturer"));
data.Add("SMBIOSBIOSVersion", HW.identifier("Win32_BIOS", "SMBIOSBIOSVersion"));
data.Add("IdentificationCode", HW.identifier("Win32_BIOS", "IdentificationCode"));
data.Add("SerialNumber", HW.identifier("Win32_BIOS", "SerialNumber"));
data.Add("ReleaseDate", HW.identifier("Win32_BIOS", "ReleaseDate"));
data.Add("Version", HW.identifier("Win32_BIOS", "Version"));
然后用字典做任何你想做的事。 对于 bios 数据来说,这可能有点矫枉过正,但如果您需要,它可以让您提取其他数据——例如主板:
data.Add("Model", HW.identifier("Win32_BaseBoard", "Model"));
data.Add("Manufacturer", HW.identifier("Win32_BaseBoard", "Manufacturer"));
data.Add("Name", HW.identifier("Win32_BaseBoard", "Name"));
data.Add("SerialNumber", HW.identifier("Win32_BaseBoard", "SerialNumber"));
或CPU
data.Add("Unique ID", HW.identifier("Win32_Processor", "UniqueId"));
data.Add("ID", HW.identifier("Win32_Processor", "ProcessorId"));
data.Add("Name", HW.identifier("Win32_Processor", "Name"));
data.Add("Manufacturer", HW.identifier("Win32_Processor", "Manufacturer"));
data.Add("MaxClockSpeed", HW.identifier("Win32_Processor", "MaxClockSpeed"));
编辑:以防万一有人需要 - here 是所有 win32 classes 的列表。