2015-02-06 80 views
1

我想爲不同的Windows操作系統獲取PC的物理USB端口的數量。首先,它看起來像一個簡單的任務,但現在我變得絕望了。是否有可能獲得PC的物理USB端口數量?

也許這個問題甚至不是有效的,因爲我不知道如何在硬件級別上處理USB端口。

我認爲使用WMI(C)ManagementObjectSearcher將是正確的路徑,它會在某些操作系統上返回正確的值。或者這就是我的想法。

例如,我嘗試以下:

// >wmic path win32_usbhub get name 

private const string UsbProperty = "Name"; 
private const string UsbPath = "Win32_USBHub"; 
private const string UsbPortName = "USB ROOT HUB"; 

... 

// Get USB Ports 
public IEnumerable<string> GetUsbPorts() 
{ 
    // All from Win32_USBHub where name contains USB ROOT HUB 
    return GetManagementObjectValues(UsbProperty, UsbPath).Where(name => 
     CultureInfo.CurrentCulture.CompareInfo.IndexOf(
      name, UsbPortName, CompareOptions.IgnoreCase) >= 0); 
} 

// Query 
private static IEnumerable<string> GetManagementObjectValues(
    string properties, string path, Func<object, string> formatter = null) 
{ 
    var values = new List<string>(); 
    string query = string.Format("SELECT {0} FROM {1}", properties, path); 
    var search = new ManagementObjectSearcher(query); 

    try 
    { 
     foreach (ManagementObject item in search.Get()) 
     { 
      string value = string.Empty; 
      foreach (string property in properties.Split(',') 
       .Select(prop => prop.Trim())) 
      { 
       if (item[property] == null) 
        continue; 

       if (value.Length > 0) 
        value += " "; 

       if (formatter != null) 
        value += formatter(item[properties]); 
       value += item[property].ToString(); 
      } 
      values.Add(value.TrimEnd()); 
     } 
    } 
    catch (Exception e) 
    { 
     if (e is ManagementException) 
      Logger.Warn(string.Format(
       "Can't extract {0} properties of {1}", properties, path), e); 
     else 
      Logger.Error(e); 
    } 

    return values.Count >= 1 ? values : new List<string> { DefaultValue }; 
} 

這似乎讓我在Windows8上的適量,但在WindowsXP它是完全關閉。

接下來,我嘗試了(例如)以下內容。我注意到在Win8上我有USB<number>作爲ExternalReferenceDesignator但在WinXP上,有簡單USBInternalReferenceDesignator和外部是空的。

對於XP這似乎工作得很好,但在Win8端口數量再次是六(6)。實際的端口數爲3,並且有擴展塢站7(7)。

// >wmic path Win32_PortConnector get ExternalReferenceDesignator,InternalReferenceDesignator 

private const string UsbPortName = "USB";  
private const string PortProperties = 
    "ExternalReferenceDesignator, InternalReferenceDesignator"; 
private const string PortPath = @"Win32_PortConnector"; 

... 

public IEnumerable<string> GetEthernetPorts() 
{ 
    // All where external includes USB or internal equals USB 
    return GetManagementObjectValues(PortProperties, PortPath).Where(name => 
     CultureInfo.CurrentCulture.CompareInfo.IndexOf(
      name, UsbPortName, CompareOptions.IgnoreCase) >= 0 || 
     string.Compare(name, UsbPortName, StringComparison.OrdinalIgnoreCase) == 0); 
}  

那麼,它甚至有可能,或者我只是從錯誤的地方看?

+0

有關? https://msdn.microsoft.com/en-ca/library/windows/hardware/ff560019%28v=vs.85%29.aspx – 2015-02-06 19:17:28

+0

可能是,我看着它。但是我需要獲得沒有外部工具的信息。 – 2015-02-06 19:24:30

+0

應該有可用的實用程序的源代碼,它可以讓您確定需要使用的api(s)。 – 2015-02-06 19:26:09

回答

2

並回答我自己的問題:不,這是不可能的。

WMIC對Win32_USBController(或某些相關路徑)的查詢結果可能看起來不錯,但您無法從中得出任何結論。有關連接器的信息不會存儲在底板或任何其他位置。

例如舊的戴爾Latitude D830與Windows XP SP3有三(3)物理連接器,但低於WMIC和USBView.exe顯示結果:

C:\>wmic path win32_usbcontroller get caption 
Caption 
Intel(R) ICH8 Family USB Universal Host Controller - 2834 
Intel(R) ICH8 Family USB Universal Host Controller - 2835 
Intel(R) ICH8 Family USB2 Enhanced Host Controller - 283A 
Intel(R) ICH8 Family USB Universal Host Controller - 2830 
Intel(R) ICH8 Family USB Universal Host Controller - 2831 
Intel(R) ICH8 Family USB Universal Host Controller - 2832 
Intel(R) ICH8 Family USB2 Enhanced Host Controller - 2836 

imgur

相關問題