C# WndProc이용, USB인식 및 해제 감시
protected override void WndProc(ref Message m) { UInt32 WM_DEVICECHANGE = 0x0219; UInt32 DBT_DEVTUP_VOLUME = 0x02; UInt32 DBT_DEVICEARRIVAL = 0x8000; UInt32 DBT_DEVICEREMOVECOMPLETE = 0x8004; if ((m.Msg == WM_DEVICECHANGE) && (m.WParam.ToInt32() == DBT_DEVICEARRIVAL)) //디바이스 연결 { int devType = Marshal.ReadInt32(m.LParam, 4); if (devType == DBT_DEVTUP_VOLUME) { RefreshDevice(); } } if ((m.Msg == WM_DEVICECHANGE) && (m.WParam.ToInt32() == DBT_DEVICEREMOVECOMPLETE)) //디바이스 연결 해제 { int devType = Marshal.ReadInt32(m.LParam, 4); if (devType == DBT_DEVTUP_VOLUME) { RefreshDevice(); } }
base.WndProc(ref m); }
|
public void RefreshDevice() { listBox1.Items.Clear(); string[] ls_drivers = System.IO.Directory.GetLogicalDrives(); //연결 되어있는 디바이스 얻어오기 foreach (string device in ls_drivers) { System.IO.DriveInfo dr = new System.IO.DriveInfo(device); if (dr.DriveType == System.IO.DriveType.Removable) //제거 가능한 타입이라면 { listBox1.Items.Add(device); } } } |