如题

解决方案 »

  1.   


            public const int WM_DEVICECHANGE = 0x219;
            public const int DBT_DEVICEARRIAL = 0x8000;
            public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
            protected override void WndProc(ref Message m)
            {
                if (m.Msg == WM_DEVICECHANGE)
                {
                    switch (m.WParam.ToInt32())
                    {
                        case DBT_DEVICEARRIAL:
                            {
                                DriveInfo[] dis = DriveInfo.GetDrives();
                                foreach (DriveInfo di in dis)
                                {
                                    if (di.DriveType == DriveType.Removable)
                                    {
                                        listBox1.Items.Add("U盘已经插入,盘符为:" + di.Name);
                                        break;
                                        
                                    }
                                }
                                break;
                                
                            }
                        case DBT_DEVICEREMOVECOMPLETE:
                            {
                                listBox1.Items.Add("U盘已经移除");
                                break;
                            }
                    }
                }
                base.WndProc(ref m);
            }
      

  2.   

    拔插靠 WM_DEVICECHANGE 事件。
    楼上已说清楚。至于串口号,一般靠驱动、驱动按装时有可以已经指定了。
    可是设备的ID去注册表中找。