我需要能够检索(通过 C# 以编程方式)通过 Windows 设备管理器获得的一些信息。具体来说,我指的是“设备属性”对话框的“详细信息”选项卡上的信息。就我而言,我需要检索 PC 上网络适配器的“位置信息”属性。理想情况下,我更愿意通过 WMI 或类似方法使用 API 调用来执行此操作,但无法找到或弄清楚如何做到这一点。无论如何,如果有人有任何关于如何使用 DevCon 或一些 API 调用来做到这一点的信息,我将非常感谢任何帮助。我在这里的另一篇文章中找到了一个代码示例(我在下面复制了它),但这并没有给我我正在寻找的信息。
private static void test()
{
ManagementPath path = new ManagementPath();
ManagementClass devs = null;
path.Server = ".";
path.NamespacePath = @"root\CIMV2";
path.RelativePath = @"Win32_PnPentity";
System.IO.File.Delete(fileName);
using (devs = new ManagementClass(new ManagementScope(path), path, new ObjectGetOptions(null, new TimeSpan(0, 0, 0, 2), true)))
{
ManagementObjectCollection moc = devs.GetInstances();
foreach (ManagementObject mo in moc)
{
try
{
PropertyDataCollection devsProperties = mo.Properties;
foreach (PropertyData devProperty in devsProperties)
{
if (devProperty.Type == CimType.DateTime)
{
if (devProperty.Value != null)
{
Console.WriteLine("Date {0}", ToDateTime(devProperty.Value.ToString()));
System.IO.File.AppendAllText(fileName, "Date " + ToDateTime(devProperty.Value.ToString()) + Environment.NewLine);
}
}
else
{
Console.WriteLine("Property = {0}\t Value = {1}", devProperty.Name, devProperty.Value);
System.IO.File.AppendAllText(fileName, "Property = " + devProperty.Name + "\t Value = " + devProperty.Value + Environment.NewLine);
}
}
if (String.IsNullOrEmpty(mo["DeviceID"].ToString()))
{
System.IO.File.AppendAllText(fileName, "Device ID was NULL" + Environment.NewLine);
Console.WriteLine("****** Device ID was NULL ******");
continue;
}
int count = 0;
RelatedObjectQuery relatedQuery;
try
{
relatedQuery = new RelatedObjectQuery("associators of {Win32_PnPEntity.DeviceID='" + mo["DeviceID"] + "'}");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(new ManagementScope(path), relatedQuery);
foreach (ManagementObject mob in searcher.Get())
{
System.IO.File.AppendAllText(fileName, "--------------------------->>>>>>" + Environment.NewLine);
Console.WriteLine("--------------------------->>>>>>");
System.IO.File.AppendAllText(fileName, mob["Description"].ToString() + Environment.NewLine);
Console.WriteLine(mob["Description"]);
++count;
}
}
catch (Exception fEx)
{
string temp = fEx.Message;
continue;
}
System.IO.File.AppendAllText(fileName, "----------------------" + Environment.NewLine);
Console.WriteLine("----------------------");
}
catch (Exception fEx)
{
string temp = fEx.Message;
}
}