0

我需要能够检索(通过 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;
                }
            }
4

1 回答 1

0

我自己想通了!我使用了此处发布的答案,并通过在他进行以下调用后添加一行代码对其进行了略微修改:

string desc = GetDevicePropertyString(pNewDevInfoSet, devInfoData, SetupDiGetDeviceRegistryPropertyEnum.SPDRP_DEVICEDESC);

我添加了以下内容:

string locationInformation = GetDevicePropertyString(pNewDevInfoSet, devInfoData, SetupDiGetDeviceRegistryPropertyEnum.SPDRP_LOCATION_INFORMATION);

非常感谢发帖并回答该帖子的原始人 (@domskey)。不确定他是否编写了所有代码。那里有很多类支持 SetupAPI.dll 调用。为我节省了大量时间!

于 2016-11-18T21:59:17.160 回答