SoFunction
Updated on 2025-03-07

C# Get USB Event API Example Analysis

This article describes the C# acquisition of USB event API. Share it for your reference. The details are as follows:

const int WM_DEVICECHANGE = 0x2190;
const int DBT_DEVICEARRIVAL = 0x8000;
const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
protected override void WndProc(ref Message m)
{
 try
 {
  //if ( == WM_DEVICECHANGE)
  //{
   switch (.ToInt32())
   {
    case DBT_DEVICEARRIVAL:  // Insert the USB drive     DriveInfo[] s = ();
     foreach (DriveInfo drive in s)
     {
      if ( == )
      {
       ("USB plug-in");
       break;
      }
     }
     break;
    case DBT_DEVICEREMOVECOMPLETE: //U disk uninstall     //
     ("USB Uninstall");
     break;
    default:
     break;
   }
  //}
 }
 catch (Exception ex)
 {
  ();
 }
 (ref m);
}

I hope this article will be helpful to everyone's C# programming.