该C#代码实现监控USB接口是否有设备接入或拨出,包括多个U盘。

 using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms; namespace MyApplication
{
public partial class Form1 : Form
{
const int WM_DEVICECHANGE = 0x0219; // 发生设备变动
const int DBT_DEVICEARRIVAL = 0x8000; // 系统检测到一个新设备
const int DBT_DEVTYP_VOLUME = 0x00000002; // 逻辑卷标
const int DBT_CONFIGCHANGECANCELED = 0x0019;
const int DBT_CONFIGCHANGED = 0x0018;
const int DBT_CUSTOMEVENT = 0x8006;
const int DBT_DEVICEQUERYREMOVE = 0x8001;
const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002; // 系统完成移除一个设备
const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
const int DBT_DEVICEREMOVEPENDING = 0x8003;
const int DBT_DEVICETYPESPECIFIC = 0x8005;
const int DBT_DEVNODES_CHANGED = 0x0007;
const int DBT_QUERYCHANGECONFIG = 0x0017;
const int DBT_USERDEFINED = 0xFFFF; [StructLayout(LayoutKind.Sequential)]
public struct DEV_BROADCAST_VOLUME
{
public int dbcv_size;
public int dbcv_devicetype;
public int dbcv_reserved;
public int dbcv_unitmask;
} public Form1()
{
InitializeComponent();
} protected override void WndProc(ref Message m)
{
if (m.Msg == WM_DEVICECHANGE)
{
switch (m.WParam.ToInt32())
{
case WM_DEVICECHANGE:
break;
case DBT_DEVICEARRIVAL://U disk upload
DriveInfo[] s = DriveInfo.GetDrives();
foreach (DriveInfo drive in s)
{
if (drive.DriveType == DriveType.Removable)
{
Console.WriteLine("==================================================");
Console.WriteLine("the U disk in! ,name:" + drive.Name.ToString());
break;
}
}
Console.WriteLine("==================================================");
break;
case DBT_CONFIGCHANGECANCELED:
break;
case DBT_CONFIGCHANGED:
break;
case DBT_CUSTOMEVENT:
break;
case DBT_DEVICEQUERYREMOVE:
break;
case DBT_DEVICEQUERYREMOVEFAILED:
break;
case DBT_DEVICEREMOVECOMPLETE: //U disk is out
DriveInfo[] I = DriveInfo.GetDrives();
foreach (DriveInfo DrInfo in I)
{
int devType = Marshal.ReadInt32(m.LParam, );
if (devType == DBT_DEVTYP_VOLUME)
{
DEV_BROADCAST_VOLUME vol;
vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME));
string ID = vol.dbcv_unitmask.ToString("x");
Console.WriteLine(IO(ID) + ": U disk out!");
}
}
break;
case DBT_DEVICEREMOVEPENDING:
break;
case DBT_DEVICETYPESPECIFIC:
break;
case DBT_DEVNODES_CHANGED:
break;
case DBT_QUERYCHANGECONFIG:
break;
case DBT_USERDEFINED:
break;
default:
break;
}
}
base.WndProc(ref m);
} public string IO(string IO)
{
string Value = "";
switch (IO)
{
case "":
Value = "A:";
break;
case "":
Value = "B:";
break;
case "":
Value = "C:";
break;
case "":
Value = "D:";
break;
case "":
Value = "E:";
break;
case "":
Value = "F:";
break;
case "":
Value = "G:";
break;
case "":
Value = "H:";
break;
case "":
Value = "I:";
break;
case "":
Value = "J:";
break;
case "":
Value = "K:";
break;
case "":
Value = "L:";
break;
case "":
Value = "M:";
break;
case "":
Value = "N:";
break;
case "":
Value = "O:";
break;
case "":
Value = "P:";
break;
case "":
Value = "Q:";
break;
case "":
Value = "R:";
break;
case "":
Value = "S:";
break;
case "":
Value = "T:";
break;
case "":
Value = "U:";
break;
case "":
Value = "V:";
break;
case "":
Value = "W:";
break;
case "":
Value = "X:";
break;
case "":
Value = "Y:";
break;
case "":
Value = "Z:";
break;
default: break;
}
return Value;
}
}
}
05-11 11:28