2013-05-31 122 views
1

我試圖在我的應用程序中實現一項功能,該功能列出了計算機中插入的所有USB海量存儲設備。用C#和WMI列出USB設備

我的代碼在啓動應用程序時運行良好,但我的問題是當我的表單中的盒子被移除或連接時,會自動刷新。

實現DBT_DEVICEARRIVAL和DBT_DEVICEREMOVECOMPLETE條件應該可以工作,但我找回了「DisconnectedContext was detected」異常。

我知道我需要使用委託並設置異步調用才能正常工作。

這裏是我的代碼:

  public void listUSB() 
     { 
     ManagementScope sc = new ManagementScope(wmiUsbList); 

     ObjectQuery query = new ObjectQuery("select * from Win32_DiskDrive " + "where InterfaceType='USB'"); 

     ManagementObjectSearcher searcher = new ManagementObjectSearcher(sc, query); 
     ManagementObjectCollection result = searcher.Get(); 

     foreach (ManagementObject obj in result) 
      { 
       if (obj["DeviceID"] != null) 
         { 
          usbListTextBox.AppendText(obj["Model"].ToString()); 
         } 
      } 

     } 

我真的很想知道如何委託適用於我的方法。

我也看了一下this MSDN上的線程,它提供了一個很好的例子,但就那個例子而言,我無法理解如何將deviceList放入文本框中。

我還在學習如果有人可以如此友善地指出我的一個或兩個問題的正確方向,那將不勝感激。

謝謝!

回答

0

嘗試使用ManagementEventWatcher併爲EventArrived分配事件處理程序。

我不知道如何完成的正是這種,但這裏是偵聽到的打印事件觀察者:

string printQuery = "Select * From __InstanceCreationEvent Within 1 Where TargetInstance ISA 'Win32_PrintJob'"; 
string nspace = @"\\.\root\CIMV2"; 
var watcher = new ManagementEventWatcher(nspace, printQuery); 

希望它能幫助。

0
private usbListArrayDelegate mDeleg; 

     protected override void WndProc(ref Message m) 
     { 
      int devType; 
      base.WndProc(ref m); 

      switch (m.WParam.ToInt32()) 
      { 
       case DBT_DEVICEARRIVAL: 

        devType = Marshal.ReadInt32(m.LParam, 4); 

        if (devType == DBT_DEVTYP_VOLUME) 
        { 
         // usb device inserted, call the query  
         mDeleg = new usbListArrayDelegate(usbListArray); 
         AsyncCallback callback = new AsyncCallback(usbListArrayCallback); 


         // invoke the thread that will handle getting the friendly names 
         mDeleg.BeginInvoke(callback, new object()); 

        } 

        break; 

       case DBT_DEVICEREMOVECOMPLETE:  

        devType = Marshal.ReadInt32(m.LParam, 4); 

        if (devType == DBT_DEVTYP_VOLUME) 
        { 
         mDeleg = new usbListArrayDelegate(usbListArray); 
         AsyncCallback callback = new AsyncCallback(usbListArrayCallback); 


         // invoke the thread that will handle getting the friendly names 
         mDeleg.BeginInvoke(callback, new object()); 
        } 

        break; 
     } 
     } 

     public ArrayList usbListArray() 
     { 
      ArrayList deviceList = new ArrayList(); 

      manager = new UsbManager(); ==> about how to implement this please see http://www.codeproject.com/Articles/63878/Enumerate-and-Auto-Detect-USB-Drives 

      UsbDiskCollection disks = manager.GetAvailableDisks(); 

      foreach (UsbDisk disk in disks) 
      { 
       deviceList.Add(disk);    
      } 

      return deviceList; 
     } 

     // delegate wrapper function for the getFriendlyNameList function 
     private delegate ArrayList usbListArrayDelegate(); 

     // callback method when the thread returns 
     private void usbListArrayCallback(IAsyncResult ar) 
     { 
      string ArrayData = string.Empty; 
      // got the returned arrayList, now we can do whatever with it 
      ArrayList result = mDeleg.EndInvoke(ar); 

      int count = 0; 

      foreach (UsbDisk disk in result) 
      { 
       ++count; 

       ArrayData += count + ") " + disk.ToString(). 
      }