"1" The knowledge points involved
1) Windows message processing functions
protected override void WndProc (ref Message m)
System hardware that captures a message changes the systems messages emitted
2) Hardware information class
DriveInfo
"2" core function
Message constants:
<summary>///Windows message constant///</summary>class cwndpromsgconst{public const int wm_devicechange = 0x219; System hardware changes emitted system message public const int dbt_devicearrival = 0x8000;//device detection ends, and public const int DBT_ can be used 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;//device unloaded or unplugged public const int dbt_deviceremovepending = 0X8003;
public const int dbt_devicetypehanged = 0x0007; public const int dbt_querychangspecific = 0x8005; public const int dbt_devnodes_ceconfig = 0x0017; public const int dbt_userdefined = 0xFFFF;}
Scan function:
<summary> //scan U-Port equipment ///</summary> private void Scanusbdisk () { _ Usbdisklist.clear (); driveinfo[] drives = Driveinfo.getdrives (); foreach (DriveInfo drive in drives) { if (drive. DriveType = = drivetype.removable) &&!drive. Name.substring (0, 1). Equals ("A")) { try { _usbdisklist.add (drive). Name); } Catch { MessageBox.Show ("The current disk is not recognized correctly, please try again!") "," hint ", MessageBoxButtons.OK, MessageBoxIcon.Information);}}}
Message Handler functions:
public void filldata (Form form, Message m, listbox listbox) {_listbox = listbox; _form = form; try {if (m.msg = = cwndpromsgconst.wm_devicechange)//System hardware changes the message sent Switch (M.wparam.toint32 ()) {case Cwndpromsgconst.wm_devicechange: Break Device detection is complete, and case Cwndpromsgconst.dbt_devicearrival can be used: { Scanusbdisk (); _listbox. Items.clear (); foreach (String str in _usbdisklist) {_listbox. Items.Add (str); }} break; Device unload or unplug Case Cwndpromsgconst.dbt_deviceremovecomplete: {scanusbdisk (); _listbox. Items.clear (); foreach (String str in _usbdisklist) {_listbox. Items.Add (str); }} break; Default:break; }}} catch (Exception ex) {MessageBox.Show ("the current disk is not recognized correctly, please re- Try! "," hint ", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
Complete CS Package file:
Using system;using system.collections.generic;using system.text;using system.windows.forms;using System.Threading; Using System.io;namespace usbmonitor{///<summary>///USB plug-in monitoring class///</summary> public class CUSBM Onitor {private delegate void Settextcallback (string s); Private ilist<string> _usbdisklist = new list<string> (); Private ListBox _listbox = null; Private Form _form = null; Public Cusbmonitor () {System.Timers.Timer Timer = new System.Timers.Timer (1000); Timer. Enabled = true; A timer occurs when the interval is reached. Elapsed + = new System.Timers.ElapsedEventHandler (timerlist); Timer. AutoReset = false; Only one time after the first end of the interval} public void Filldata (Form form, Message m, listbox listbox) { _listbox = ListBox; _form = form; try {if (m.msg = = cwndpromsgconst.wm_devicechange)//SystemSystem message issued by hardware change {switch (M.wparam.toint32 ()) {CA Se CWndProMsgConst.WM_DEVICECHANGE:break; Device detection is complete, and case Cwndpromsgconst.dbt_devicearrival can be used: { Scanusbdisk (); _listbox. Items.clear (); foreach (String str in _usbdisklist) {_listbox. Items.Add (str); }} break; Device uninstall or unplug case Cwndpromsgconst.dbt_deviceremovecomplete: { Scanusbdisk (); _listbox. Items.clear (); foreach (String str iN _usbdisklist) {_listbox. Items.Add (str); }} break; Default:break; }}} catch (Exception ex) {MessageBox.Show ("the current disk is not recognized correctly, please re- Try! "," hint ", MessageBoxButtons.OK, MessageBoxIcon.Information); }}///<summary>//Set USB list///</summary> void Timerlist (obje CT sender, System.Timers.ElapsedEventArgs e) {scanusbdisk (); foreach (String str in _usbdisklist) {SetText (str); }}///<summary>//Scan U-Port equipment///</summary> private void Scanusbdisk () {_usbdisklist.clear (); Driveinfo[] DRives = Driveinfo.getdrives (); foreach (DriveInfo drive in drives) {if (drive. DriveType = = drivetype.removable) &&!drive. Name.substring (0, 1). Equals ("A")) {try {_usbdisklist.add (drive). Name); } catch {MessageBox.Show ("the current disk is not recognized correctly, please try again!") "," hint ", MessageBoxButtons.OK, MessageBoxIcon.Information); }}}///<summary>//Set up list///</summary> <param name= "text" > name </param> public void SetText (string text) {if (_listbox = = NULL) return; if (This._listbox. invokerequired)//Call bearing {if (_listbox) in a thread other than the thread on which the control is created. Items.contains (text)) return; Settextcallback d = new SETTEXTCALlback (SetText); _form. Invoke (d, new object[] {text}); } else {if (_listbox. Items.contains (text)) return; This._listbox. Items.Add (text); }}}///<summary>//Windows message constants///</summary> class Cwndpromsgconst {Pub Lic const int wm_devicechange = 0x219; System hardware changes emitted system message Public const int DBT_DEVICEARRIVAL = 0x8000;//device detection ends, and public const int Dbt_configcha can be used ngecanceled = 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;//device unload or unplug public const int dbt_deviceremovepending = 0x8003; public const int dbt_devicetypehanged = 0x0007; Public const int DBT_QUERYCHANGSPECIFIC = 0x8005; public const int dbt_devnodes_ceconfig = 0x0017; public const int dbt_userdefined = 0xFFFF; } }
Test form (overriding message function):
Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.linq;using system.text;using system.windows.forms;namespace usbmonitor{public partial class Main: Form {public Main () { InitializeComponent (); } Cusbmonitor usbmonitor = new Cusbmonitor (); protected override void WndProc (ref Message m) { Usbmonitor.filldata (this, M, _listbox); Base. WndProc (ref m);}}
c#.net u disk Plug and draw monitoring