public const int DBT_DEVICEREMOVECOMPLETE = 0x8004; //一个设备或媒体片已被删除。 protected override void WndProc(ref Message m) { if (m.Msg == WM_DEVICECHANGE) { switch (m.WParam.ToInt32()) { case DBT_DEVICEARRIVAL: //U盘插入 if (UisShow) { DriveInfo[] uin = DriveInfo.GetDrives(); foreach (DriveInfo drive in uin) { if (drive.DriveType == DriveType.Removable) { int i; for (i = 0; i < Us.Count; i++) { if (Us[i].tag.Equals(drive.Name.ToString())) //找到盘符 { break; } } if (i == Us.Count) //显示U盘悬浮窗 { Form12 U = new Form12(); U.tag = drive.Name.ToString(); U.location = new System.Drawing.Point(Screen.PrimaryScreen.Bounds.Width - 300, top); top += 93; //U.isOpen = false; U.U_name = drive.Name.ToString() + " " + drive.VolumeLabel + "\n(" + Math.Round(drive.AvailableFreeSpace / 1098907648.0, 2) + "G)可用"; Us.Add(U); U.Show(); } //break; } } } break; case DBT_DEVICEREMOVECOMPLETE: //U盘卸载 if (UisShow) { top -= 93; if (Us.Count == 1) { Us[0].Dispose(); } else if (Us.Count >= 2) { //找出拔出的U盘并释放 int i, j; for (i = 0; i < Us.Count; i++) { DriveInfo[] s = DriveInfo.GetDrives(); for (j = 0; j < s.Length; j++) { if (s[j].DriveType == DriveType.Removable) { if (Us[i].tag.Equals(s[j].Name.ToString())) { break; } } } if (j == s.Length) //表明没有找到对应盘符 Us[i] { Us[i].Close(); //释放Us[i] Us[i].Dispose(); Us.Remove(Us[i]); } } } } //new Form3("U盘已拔出").ShowDialog(); //label1.Text = "请插入认证U盘"; bool bo = true; DriveInfo[] uin1 = DriveInfo.GetDrives(); //if (uin1.Equals(null)) //bo = true; foreach (DriveInfo drive in uin1) { if (pf.Equals(drive.Name.ToString())) { bo = false; } // break; } if (f2 != null && !f2.IsDisposed) { bo = false; } if (bo) { //Form1.Us this.Hide(); f2 = new Form2(); f2.Show(); //f1 = new Form1(); //f1.Show(); } break; default: break; } } base.WndProc(ref m); }