我需要能够通过Windows设备管理器检索(通过C#以编程方式)提供的一些信息。具体来说,我指的是“设备属性”对话框的“详细信息”选项卡上的信息。在我的情况下,我需要检索"位置信息" PC上的网络适配器的属性。理想情况下,我更喜欢使用通过WMI或类似的API调用来做到这一点,但是我们无法找到或弄清楚如何做到这一点。无论如何,如果有人有任何关于如何使用DevCon或一系列API调用的信息,我将非常感谢任何帮助。我在另一篇帖子here(我在下面复制过)中找到了一个代码示例,但这并没有给我提供我正在寻找的信息。
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;
}
}
答案 0 :(得分:0)
我自己想通了!我使用了here发布的答案,并通过在接下来的电话之后添加一行代码稍加修改:
string desc = GetDevicePropertyString(pNewDevInfoSet, devInfoData, SetupDiGetDeviceRegistryPropertyEnum.SPDRP_DEVICEDESC);
我添加了以下内容:
string locationInformation = GetDevicePropertyString(pNewDevInfoSet, devInfoData, SetupDiGetDeviceRegistryPropertyEnum.SPDRP_LOCATION_INFORMATION);
非常感谢发布并回复该帖子的原始人(@domskey)。不确定他是否写了所有代码。那里有很多类来支持SetupAPI.dll调用。节省了我很多时间!