簡體   English   中英

使用 C# 和 WMI 列出 USB 設備

[英]Listing USB Devices with C# and WMI

我正在嘗試在我的應用程序中實現一個功能,該功能列出了計算機中所有插入的 USB 大容量存儲設備。

我的代碼在啟動應用程序時運行良好,但我的問題是我希望表單中的框在移除或連接 USB 設備時自動刷新。

實施 DBT_DEVICEARRIVAL 和 DBT_DEVICEREMOVECOMPLETE 條件應該可以工作,但我返回“檢測到 DisconnectedContext”異常。

我了解到我需要使用委托並設置異步調用才能使其正常工作。

這是我的代碼:

          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());
                        }
            }

      }

我真的很想知道如何將委托應用於我的方法。

我還在 MSDN 上查看了這個線程,它提供了一個很好的示例,但在該示例中,我無法理解如何將 deviceList 放入文本框中。

我仍在學習,所以如果有人能在我的一個或兩個問題上為我指出正確的方向,我將不勝感激。

謝謝!

嘗試使用ManagementEventWatcher並將事件處理程序分配給EventArrived

我不知道如何完全實現這一點,但這里有一個監聽打印事件的觀察者:

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

希望能幫助到你。

   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().
           }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM