2016-11-18 21 views
0

我需要能夠通過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

我想出了自己!我用了答案貼here,只是之後在那裏,他下面的調用通過添加一行代碼稍加修改它:

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

我增加了以下內容:

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

非常感謝給原始人(@ domskey)發佈並回復該帖子。不知道他是否寫了所有的代碼。有相當多的類支持SetupAPI.dll調用。爲我節省了大量的時間!

相關問題