我有using System.Management;
但是,我不断收到此错误:
命名空间“System.Management”中不存在类型或命名空间名称“ManagementClass”(您是否缺少程序集引用?)
我两次收到该错误。
我也收到此错误:
命名空间“System.Management”中不存在类型或命名空间名称“ManagementObjectCollection”(您是否缺少程序集引用?)
为什么会这样?
如果有帮助,这是我的代码(完全取自 StackOverflow,但仍然是我正在使用的代码)
private string identifier(string wmiClass, string wmiProperty)
//Return a hardware identifier
{
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;
}
private void getButton_Click(object sender, EventArgs e)
{
string modelNo = identifier("Win32_DiskDrive", "Model");
string manufatureID = identifier("Win32_DiskDrive", "Manufacturer");
string signature = identifier("Win32_DiskDrive", "Signature");
string totalHeads = identifier("Win32_DiskDrive", "TotalHeads");
}