c# 檢測優盤插入,隨身碟拔出!

來源:互聯網
上載者:User

包括多個優盤。

 

#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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.