C#监控USB接口
该C#代码实现监控USB接口是否有设备接入或拨出,包括多个U盘。
1 using System; 2 using System.IO; 3 using System.Runtime.InteropServices; 4 using System.Windows.Forms; 5 6 namespace MyApplication 7 { 8 public partial class Form1 : Form 9 { 10 const int WM_DEVICECHANGE = 0x0219; // 发生设备变动 11 const int DBT_DEVICEARRIVAL = 0x8000; // 系统检测到一个新设备 12 const int DBT_DEVTYP_VOLUME = 0x00000002; // 逻辑卷标 13 const int DBT_CONFIGCHANGECANCELED = 0x0019; 14 const int DBT_CONFIGCHANGED = 0x0018; 15 const int DBT_CUSTOMEVENT = 0x8006; 16 const int DBT_DEVICEQUERYREMOVE = 0x8001; 17 const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002; // 系统完成移除一个设备 18 const int DBT_DEVICEREMOVECOMPLETE = 0x8004; 19 const int DBT_DEVICEREMOVEPENDING = 0x8003; 20 const int DBT_DEVICETYPESPECIFIC = 0x8005; 21 const int DBT_DEVNODES_CHANGED = 0x0007; 22 const int DBT_QUERYCHANGECONFIG = 0x0017; 23 const int DBT_USERDEFINED = 0xFFFF; 24 25 [StructLayout(LayoutKind.Sequential)] 26 public struct DEV_BROADCAST_VOLUME 27 { 28 public int dbcv_size; 29 public int dbcv_devicetype; 30 public int dbcv_reserved; 31 public int dbcv_unitmask; 32 } 33 34 public Form1() 35 { 36 InitializeComponent(); 37 } 38 39 protected override void WndProc(ref Message m) 40 { 41 if (m.Msg == WM_DEVICECHANGE) 42 { 43 switch (m.WParam.ToInt32()) 44 { 45 case WM_DEVICECHANGE: 46 break; 47 case DBT_DEVICEARRIVAL://U disk upload 48 DriveInfo[] s = DriveInfo.GetDrives(); 49 foreach (DriveInfo drive in s) 50 { 51 if (drive.DriveType == DriveType.Removable) 52 { 53 Console.WriteLine("=================================================="); 54 Console.WriteLine("the U disk in! ,name:" + drive.Name.ToString()); 55 break; 56 } 57 } 58 Console.WriteLine("=================================================="); 59 break; 60 case DBT_CONFIGCHANGECANCELED: 61 break; 62 case DBT_CONFIGCHANGED: 63 break; 64 case DBT_CUSTOMEVENT: 65 break; 66 case DBT_DEVICEQUERYREMOVE: 67 break; 68 case DBT_DEVICEQUERYREMOVEFAILED: 69 break; 70 case DBT_DEVICEREMOVECOMPLETE: //U disk is out 71 DriveInfo[] I = DriveInfo.GetDrives(); 72 foreach (DriveInfo DrInfo in I) 73 { 74 int devType = Marshal.ReadInt32(m.LParam, 4); 75 if (devType == DBT_DEVTYP_VOLUME) 76 { 77 DEV_BROADCAST_VOLUME vol; 78 vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME)); 79 string ID = vol.dbcv_unitmask.ToString("x"); 80 Console.WriteLine(IO(ID) + ": U disk out!"); 81 } 82 } 83 break; 84 case DBT_DEVICEREMOVEPENDING: 85 break; 86 case DBT_DEVICETYPESPECIFIC: 87 break; 88 case DBT_DEVNODES_CHANGED: 89 break; 90 case DBT_QUERYCHANGECONFIG: 91 break; 92 case DBT_USERDEFINED: 93 break; 94 default: 95 break; 96 } 97 } 98 base.WndProc(ref m); 99 } 100 101 public string IO(string IO) 102 { 103 string Value = ""; 104 switch (IO) 105 { 106 case "1": 107 Value = "A:"; 108 break; 109 case "2": 110 Value = "B:"; 111 break; 112 case "4": 113 Value = "C:"; 114 break; 115 case "8": 116 Value = "D:"; 117 break; 118 case "10": 119 Value = "E:"; 120 break; 121 case "20": 122 Value = "F:"; 123 break; 124 case "40": 125 Value = "G:"; 126 break; 127 case "80": 128 Value = "H:"; 129 break; 130 case "100": 131 Value = "I:"; 132 break; 133 case "200": 134 Value = "J:"; 135 break; 136 case "400": 137 Value = "K:"; 138 break; 139 case "800": 140 Value = "L:"; 141 break; 142 case "1000": 143 Value = "M:"; 144 break; 145 case "2000": 146 Value = "N:"; 147 break; 148 case "4000": 149 Value = "O:"; 150 break; 151 case "8000": 152 Value = "P:"; 153 break; 154 case "10000": 155 Value = "Q:"; 156 break; 157 case "20000": 158 Value = "R:"; 159 break; 160 case "40000": 161 Value = "S:"; 162 break; 163 case "80000": 164 Value = "T:"; 165 break; 166 case "100000": 167 Value = "U:"; 168 break; 169 case "200000": 170 Value = "V:"; 171 break; 172 case "400000": 173 Value = "W:"; 174 break; 175 case "800000": 176 Value = "X:"; 177 break; 178 case "1000000": 179 Value = "Y:"; 180 break; 181 case "2000000": 182 Value = "Z:"; 183 break; 184 default: break; 185 } 186 return Value; 187 } 188 } 189 }