private void Form1_Shown(object sender, EventArgs e) { DriveInfo[] s = DriveInfo.GetDrives(); foreach (DriveInfo drive in s) { if (drive.DriveType == DriveType.Removable) { Upanel U = new Upanel(); U.Location = new System.Drawing.Point(Screen.PrimaryScreen.Bounds.Width - 300, top); top += 93; U.isOpen = false; U.tag = drive.Name.ToString(); U.u_name = drive.Name.ToString() + " " + drive.VolumeLabel + "\n(" + Math.Round(drive.AvailableFreeSpace / 1098907648.0, 2) + "G)可用"; this.Controls.Add(U); Us.Add(U); } } }
protected override void WndProc(ref Message m) { if (m.Msg == WM_DEVICECHANGE) { switch (m.WParam.ToInt32()) { case DBT_DEVICEARRIVAL: //U盘插入 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) { pf = drive.Name.ToString(); //U盘 盘符 Upanel U = new Upanel(); 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)可用"; this.Controls.Add(U); Us.Add(U); label1.Text = "U盘已插入,正在检测..."; new Form3("U盘已插入").ShowDialog(); //Form4.pf = drive.Name.ToString(); //U盘 盘符 if (f2.Key == false) //key 为false 表示未验证成功 { this.U_Disks(); //U盘验证 } } //break; } } break; case DBT_DEVICEREMOVECOMPLETE: //U盘卸载 top -= 93; if (Us.Count == 1) { Us[0].Dispose(); Us.Remove(Us[0]); } 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].Dispose(); //释放Us[i] Us.Remove(Us[i]); } } } new Form3("U盘已拔出").ShowDialog(); label1.Text = "请插入认证U盘"; break; default: break; } } base.WndProc(ref m); }