-1
我使用下面的代碼獲取USB設備列表。但是,當我運行時沒有調試它顯示錯誤:該應用程序稱爲一個接口,被編組爲不同的線程。當獲取USB_Hub的列表
該應用程序稱爲一個接口,被編組爲一個不同的線程。
但是,當我運行應用程序調試它工作正常。我在單獨的類庫項目中使用此函數並在WPF窗體上調用它。
public static List<USBDeviceInfo> GetUSBDevices()
{
List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\cimv2");
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_USBHub");
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
//ManagementObjectCollection collection;
//using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
//var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub");
//collection = searcher.Get();
foreach (ManagementObject device in queryCollection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description"),
(string)device.GetPropertyValue("Name")
));
}
}
catch (ManagementException MEEx)
{
throw MEEx;
}
catch(COMException COMEx)
{
throw COMEx;
}
finally
{
}
//collection.Dispose();
return devices;
}
public class USBDeviceInfo
{
public USBDeviceInfo(string deviceID, string pnpDeviceID, string description, string name)
{
this.DeviceID = deviceID;
this.PnpDeviceID = pnpDeviceID;
this.Description = description;
this.Name = name;
}
public string DeviceID { get; private set; }
public string PnpDeviceID { get; private set; }
public string Description { get; private set; }
public string Name { get; private set; }
}
你使用數據綁定嗎?什麼行引發異常?你應該在'catch'子句中使用'throw;'而不是'throw MEEx;'和'throw COMEx;'。 –
沒有@luri Farenzena – Shantnu
searcher.Get()顯示錯誤,當我沒有使用任何異常。當我使用異常它顯示在投擲MEEx @luri Farenzena – Shantnu