컴퓨터에 USB 장치가 추가 또는 해제됐을 경우 확인 할 수 있는 코드입니다.
protected override void WndProc(ref Message m)
{
UInt32 WM_DEVICECHANGE = 0x0219;
UInt32 DBT_DEVTUP_VOLUME = 0x02; //저장 장치
UInt32 DBT_DEVTYP_PORT = 0x03; //포트 장치
UInt32 DBT_DEVICEARRIVAL = 0x8000;
UInt32 DBT_DEVICEREMOVECOMPLETE = 0x8004;
if ((m.Msg == WM_DEVICECHANGE) && (m.WParam.ToInt32() == DBT_DEVICEARRIVAL))//디바이스 연결
{
int devType = System.Runtime.InteropServices.Marshal.ReadInt32(m.LParam, 4);
if (devType == DBT_DEVTUP_VOLUME)
{
MessageBox.Show("저장장치 연결");
}
}
if((m.Msg == WM_DEVICECHANGE) &&(m.WParam.ToInt32() == DBT_DEVICEREMOVECOMPLETE)) //디바이스 연결 해제
{
int devType = System.Runtime.InteropServices.Marshal.ReadInt32(m.LParam, 4);
if (devType == DBT_DEVTUP_VOLUME)
{
MessageBox.Show("저장장치 연결해제");
}
else if(devType == DBT_DEVTYP_PORT)
{
string portName = System.Runtime.InteropServices.Marshal.PtrToStringAuto((IntPtr)((long)m.LParam + 12)); //연결해제된 PORT NAME
//ex.) USB To Serial 장비 등등
MessageBox.Show("PORT장치 연결해제");
}
}
base.WndProc(ref m);
}
'C#' 카테고리의 다른 글
[C#/PDA]datagrid 항목 추가(Row Add) 및 중복확인 예제 (0) | 2016.02.24 |
---|---|
[C#/PDA]SQLite LIMIT 사용 예제 (0) | 2016.02.24 |
[C#/PDA]Get Serial Port Name 예제 (0) | 2016.02.23 |
PDA 동기화(Windows Mobile Device Center) 다운로드 (0) | 2016.02.19 |
[C#]Excel 파일의 Sheet 이름 얻어오기 예제 (0) | 2016.02.19 |