C# 監聽USB

  1.         public const int WM_DEVICECHANGE = 0x219;
  2.         public const int DBT_DEVICEARRIVAL = 0x8000;
  3.         public const int DBT_CONFIGCHANGECANCELED = 0x0019;
  4.         public const int DBT_CONFIGCHANGED = 0x0018;
  5.         public const int DBT_CUSTOMEVENT = 0x8006;
  6.         public const int DBT_DEVICEQUERYREMOVE = 0x8001;
  7.         public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002;
  8.         public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
  9.         public const int DBT_DEVICEREMOVEPENDING = 0x8003;
  10.         public const int DBT_DEVICETYPESPECIFIC = 0x8005;
  11.         public const int DBT_DEVNODES_CHANGED = 0x0007;
  12.         public const int DBT_QUERYCHANGECONFIG = 0x0017;
  13.         public const int DBT_USERDEFINED = 0xFFFF;
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.         private void Form1_Load(object sender, EventArgs e)
  19.         {
  20.         }
  21.         protected override void WndProc(ref   Message m)
  22.         {
  23.             try
  24.             {
  25.                 if (m.Msg == WM_DEVICECHANGE)
  26.                 {
  27.                     switch (m.WParam.ToInt32())
  28.                     {
  29.                         case WM_DEVICECHANGE:
  30.                             break;
  31.                         case DBT_DEVICEARRIVAL://U盤插入   
  32.                             DriveInfo[] s = DriveInfo.GetDrives();
  33.                             foreach (DriveInfo drive in s)
  34.                             {
  35.                                 if (drive.DriveType == DriveType.Removable)
  36.                                 {
  37.                                     listBox1.Items.Add("U盤已插入,盤符爲:"
  38.                                                        + drive.Name.ToString());
  39.                                     break;
  40.                                 }
  41.                             }
  42.                             break;
  43.                         case DBT_CONFIGCHANGECANCELED:
  44.                             break;
  45.                         case DBT_CONFIGCHANGED:
  46.                             break;
  47.                         case DBT_CUSTOMEVENT:
  48.                             break;
  49.                         case DBT_DEVICEQUERYREMOVE:
  50.                             break;
  51.                         case DBT_DEVICEQUERYREMOVEFAILED:
  52.                             break;
  53.                         case DBT_DEVICEREMOVECOMPLETE:   //U盤卸載   
  54.                             break;
  55.                         case DBT_DEVICEREMOVEPENDING:
  56.                             break;
  57.                         case DBT_DEVICETYPESPECIFIC:
  58.                             break;
  59.                         case DBT_DEVNODES_CHANGED:
  60.                             break;
  61.                         case DBT_QUERYCHANGECONFIG:
  62.                             break;
  63.                         case DBT_USERDEFINED:
  64.                             break;
  65.                         default:
  66.                             break;
  67.                     }
  68.                 }
  69.             }
  70.             catch (Exception ex)
  71.             {
  72.                 MessageBox.Show(ex.Message);
  73.             }
  74.             base.WndProc(ref   m);
  75.         }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章