http://tjstory.tistory.com/102

 

protected override void WndProc(ref Message m)

{

                UInt32 WM_DEVICECHANGE    = 0x0219;

                UInt32 DBT_DEVTUP_VOLUME = 0x02;

                UInt32 DBT_DEVICEARRIVAL = 0x8000;

   

                if ((m.Msg == WM_DEVICECHANGE) && (m.WParam.ToInt32() == DBT_DEVICEARRIVAL))

                {

                    MessageBox.Show("디바이스가 연결되었습니다.");

   

                    int m_Count = 0;

                    int devType = Marshal.ReadInt32(m.LParam, 4);

   

                    if (devType == DBT_DEVTUP_VOLUME)

                    {

                        MessageBox.Show("USB 장치가 연결 됨");

                    }

                    else

                    {

                        MessageBox.Show("USB to COM 장치가 연결 됨");

                        //단순하게 test하기 위해 젤 마지막 포트이름을 뿌려 봄

                        foreach (string str in SerialPort.GetPortNames())

                        {    

                               //........블러블러 COM으로 Device Chage 잡힐경우

                                MessageBox.Show("현재 연결된 디바이스 포트는 : " + str);

                       }

                   }//.....etc....

               }

               base.WndProc(ref m);

 }