1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
| using System;
using System.Collections.Generic; using System.ComponentModel;
using System.Data; using System.Drawing;
using System.Text; using System.IO;
using System.Windows.Forms; using System.Runtime.InteropServices;
namespace USB {
public partial class USB : Form {
public USB() {
InitializeComponent(); }
public const int WM_DEVICECHANGE = 0x219; public const int DBT_DEVICEARRIVAL = 0x8000;
public const int DBT_CONFIGCHANGECANCELED = 0x0019; public const int DBT_CONFIGCHANGED = 0x0018;
public const int DBT_CUSTOMEVENT = 0x8006; public const int DBT_DEVICEQUERYREMOVE = 0x8001;
public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002; public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
public const int DBT_DEVICEREMOVEPENDING = 0x8003; public const int DBT_DEVICETYPESPECIFIC = 0x8005;
public const int DBT_DEVNODES_CHANGED = 0x0007; public const int DBT_QUERYCHANGECONFIG = 0x0017;
public const int DBT_USERDEFINED = 0xFFFF; // 逻辑卷标
public const int DBT_DEVTYP_VOLUME = 0x00000002; // private LockScreen Ls = new LockScreen();
public string ID = ""; public string Value;
public string[] item;
[StructLayout(LayoutKind.Sequential)] public struct DEV_BROADCAST_VOLUME
{ public int dbcv_size;
public int dbcv_devicetype; public int dbcv_reserved;
public int dbcv_unitmask; }
protected override void WndProc(ref Message m)
{ try
{ if (m.Msg == WM_DEVICECHANGE)
{ switch (m.WParam.ToInt32())
{ case WM_DEVICECHANGE:
break; case DBT_DEVICEARRIVAL://U盘有插入
this.timer1.Enabled = true; DriveInfo[] s = DriveInfo.GetDrives();
foreach (DriveInfo DriveI in s) {
if (DriveI.DriveType == DriveType.Removable) {
// Ls.Show(); // this.Hide();
// MessageBox.Show("sss");
break; }
int devType = Marshal.ReadInt32(m.LParam, 4); if (devType == DBT_DEVTYP_VOLUME)
{ DEV_BROADCAST_VOLUME vol;
vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME)); ID = vol.dbcv_unitmask.ToString("x");
this.Text = IO(ID); this.Tag = IO(ID);
//if (item.Length ==0||IO(ID)!=this.Tag.ToString ()) //{
//}
} this.label1.Text = this.Text;
} 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盘卸载 DriveInfo[] I = DriveInfo.GetDrives();
foreach (DriveInfo DrInfo in I) {
int devType = Marshal.ReadInt32(m.LParam, 4); if (devType == DBT_DEVTYP_VOLUME)
{ DEV_BROADCAST_VOLUME vol;
vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME)); ID = vol.dbcv_unitmask.ToString("x");
this.Text = IO(ID) + "盘退出!\n";
}
this.label1.Text += this.Text; // MessageBox.Show("U盘已经卸载", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
} break;
case DBT_DEVICEREMOVEPENDING: break;
case DBT_DEVICETYPESPECIFIC: break;
case DBT_DEVNODES_CHANGED: break;
case DBT_QUERYCHANGECONFIG: break;
case DBT_USERDEFINED: break;
default: break;
} }
} catch (Exception ex)
{ throw new Exception(ex.Message);
} base.WndProc(ref m);
}
private void USB_Load(object sender, EventArgs e) {
}
public string IO(string ff) {
switch (ff) {
case "1": Value = "A:";
break; case "2":
Value = "B:"; break;
case "4": Value = "C:";
break; case "8":
Value = "D:"; break;
case "10": Value = "E:";
break; case "20":
Value = "F:"; break;
case "40": Value = "G:";
break; case "80":
Value = "H:"; break;
case "100": Value = "I:";
break; case "200":
Value = "J:"; break;
case "400": Value = "K:";
break; case "800":
Value = "L:"; break;
case "1000": Value = "M:";
break; case "2000":
Value = "N:"; break;
case "4000": Value = "O:";
break; case "8000":
Value = "P:"; break;
case "10000": Value = "Q:";
break; case "20000":
Value = "R:"; break;
case "40000": Value = "S:";
break; case "80000":
Value = "T:"; break;
case "100000": Value = "U:";
break; case "200000":
Value = "V:"; break;
case "400000": Value = "W:";
break; case "800000":
Value = "X:"; break;
case "1000000": Value = "Y:";
break; case "2000000":
Value = "Z:"; break;
default: break; }
return Value; }
private void timer1_Tick(object sender, EventArgs e)
{ // this.Text = ID;
} }
}
|