包括多個優盤。
#region USB Device Check start<br /> //======================================================================================<br /> //Search the USB device</p><p> public const int WM_DEVICECHANGE = 0x219;<br /> public const int DBT_DEVICEARRIVAL = 0x8000;<br /> public const int DBT_CONFIGCHANGECANCELED = 0x0019;<br /> public const int DBT_CONFIGCHANGED = 0x0018;<br /> public const int DBT_CUSTOMEVENT = 0x8006;<br /> public const int DBT_DEVICEQUERYREMOVE = 0x8001;<br /> public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002;<br /> public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;<br /> public const int DBT_DEVICEREMOVEPENDING = 0x8003;<br /> public const int DBT_DEVICETYPESPECIFIC = 0x8005;<br /> public const int DBT_DEVNODES_CHANGED = 0x0007;<br /> public const int DBT_QUERYCHANGECONFIG = 0x0017;<br /> public const int DBT_USERDEFINED = 0xFFFF;</p><p> public const int DBT_DEVTYP_VOLUME = 0x00000002;</p><p> [StructLayout(LayoutKind.Sequential)]<br /> public struct DEV_BROADCAST_VOLUME<br /> {<br /> public int dbcv_size;<br /> public int dbcv_devicetype;<br /> public int dbcv_reserved;<br /> public int dbcv_unitmask;<br /> }</p><p> protected override void WndProc(ref Message m)<br /> {<br /> if (m.Msg == WM_DEVICECHANGE)<br /> {<br /> switch (m.WParam.ToInt32())<br /> {<br /> case WM_DEVICECHANGE:<br /> break;<br /> case DBT_DEVICEARRIVAL://U disk upload<br /> DriveInfo[] s = DriveInfo.GetDrives();</p><p> foreach (DriveInfo drive in s)<br /> {<br /> if (drive.DriveType == DriveType.Removable)<br /> {<br /> Console.WriteLine("==================================================");<br /> Console.WriteLine("the U disk in! ,name:" + drive.Name.ToString());<br /> break;<br /> }<br /> }<br /> Console.WriteLine("==================================================");<br /> break;<br /> case DBT_CONFIGCHANGECANCELED:<br /> break;<br /> case DBT_CONFIGCHANGED:<br /> break;<br /> case DBT_CUSTOMEVENT:<br /> break;<br /> case DBT_DEVICEQUERYREMOVE:<br /> break;<br /> case DBT_DEVICEQUERYREMOVEFAILED:<br /> break;<br /> case DBT_DEVICEREMOVECOMPLETE: //U disk is out<br /> DriveInfo[] I = DriveInfo.GetDrives();</p><p> foreach (DriveInfo DrInfo in I)<br /> {<br /> int devType = Marshal.ReadInt32(m.LParam, 4);</p><p> if (devType == DBT_DEVTYP_VOLUME)<br /> {<br /> DEV_BROADCAST_VOLUME vol;<br /> vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME));<br /> string ID = vol.dbcv_unitmask.ToString("x");<br /> Console.WriteLine(IO(ID) + ": U disk out!");<br /> }<br /> }</p><p> break;<br /> case DBT_DEVICEREMOVEPENDING:<br /> break;<br /> case DBT_DEVICETYPESPECIFIC:<br /> break;<br /> case DBT_DEVNODES_CHANGED:<br /> break;<br /> case DBT_QUERYCHANGECONFIG:<br /> break;<br /> case DBT_USERDEFINED:<br /> break;<br /> default:<br /> break;</p><p> }<br /> }<br /> base.WndProc(ref m);<br /> }</p><p> public string IO(string ff)<br /> {<br /> string Value = "";<br /> switch (ff)<br /> {<br /> case "1":<br /> Value = "A:";<br /> break;<br /> case "2":<br /> Value = "B:";<br /> break;<br /> case "4":<br /> Value = "C:";<br /> break;<br /> case "8":<br /> Value = "D:";<br /> break;<br /> case "10":<br /> Value = "E:";<br /> break;<br /> case "20":<br /> Value = "F:";<br /> break;<br /> case "40":<br /> Value = "G:";<br /> break;<br /> case "80":<br /> Value = "H:";<br /> break;<br /> case "100":<br /> Value = "I:";<br /> break;<br /> case "200":<br /> Value = "J:";<br /> break;<br /> case "400":<br /> Value = "K:";<br /> break;<br /> case "800":<br /> Value = "L:";<br /> break;<br /> case "1000":<br /> Value = "M:";<br /> break;<br /> case "2000":<br /> Value = "N:";<br /> break;<br /> case "4000":<br /> Value = "O:";<br /> break;<br /> case "8000":<br /> Value = "P:";<br /> break;<br /> case "10000":<br /> Value = "Q:";<br /> break;<br /> case "20000":<br /> Value = "R:";<br /> break;<br /> case "40000":<br /> Value = "S:";<br /> break;<br /> case "80000":<br /> Value = "T:";<br /> break;<br /> case "100000":<br /> Value = "U:";<br /> break;<br /> case "200000":<br /> Value = "V:";<br /> break;<br /> case "400000":<br /> Value = "W:";<br /> break;<br /> case "800000":<br /> Value = "X:";<br /> break;<br /> case "1000000":<br /> Value = "Y:";<br /> break;<br /> case "2000000":<br /> Value = "Z:";<br /> break;<br /> default: break;<br /> }<br /> return Value;<br /> }</p><p>#endregion USB Device Check End