萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> asp.net編程 >> C#獲取USB事件API

C#獲取USB事件API

 const int WM_DEVICECHANGE = 0x2190;
const int DBT_DEVICEARRIVAL = 0x8000;
const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
protected override void WndProc(ref Message m)
{
try
{
//if (m.Msg == WM_DEVICECHANGE)
//{
switch (m.WParam.ToInt32())
{
case DBT_DEVICEARRIVAL: // U盤插入
DriveInfo[] s = DriveInfo.GetDrives();
foreach (DriveInfo drive in s)
{
if (drive.DriveType == DriveType.Removable)
{
Console.WriteLine("USB插入");
break;
}
}
break;
case DBT_DEVICEREMOVECOMPLETE: //U盤卸載
//
Console.WriteLine("USB卸載");
break;
default:
break;
}
//}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
base.WndProc(ref m);
}

copyright © 萬盛學電腦網 all rights reserved