//获取USB使用信息 private void button1_Click(object sender, EventArgs e) { //清空内容 richTextBox1.Clear(); //定义注册表顶级节点 其命名空间是using Microsoft.Win32; RegistryKey USBKey; //检索子项 USBKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Enum\USBSTOR", false); //记录获取数量赋初值 Num = 0; //检索所有子项USBSTOR下的字符串数组 foreach (string sub1 in USBKey.GetSubKeyNames()) { RegistryKey sub1key = USBKey.OpenSubKey(sub1, false); foreach (string sub2 in sub1key.GetSubKeyNames()) { try { /* OpenSubKey 检索子项 函数原型 * public RegistryKey OpenSubKey( * string name, //要打开的子项名称或路径 * bool writable //如果需要项的写访问权限=true * ) */ //打开sub1key的子项 RegistryKey sub2key = sub1key.OpenSubKey(sub2, false); //检索Service=disk(磁盘)值的子项 cdrom(光盘) if (sub2key.GetValue("Service", "").Equals("disk")) { String Path = "USBSTOR" + "\\" + sub1 + "\\" + sub2; String Name = (string)sub2key.GetValue("FriendlyName", ""); string fileName = "usbDeleteLog.txt"; //文件名称 bool isDelete = false; //是否删除 string content = ""; //文件内容 //判断是否文件存在 if (!File.Exists(fileName)) { //创建写入文件 FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write); StreamWriter sw = new StreamWriter(fs); sw.WriteLine(); sw.Close(); fs.Close(); } else { StreamReader sReader = new StreamReader(fileName); content = sReader.ReadToEnd(); isDelete = content.Contains(Path); sReader.Close(); } //不存在 if (isDelete == false) { //记录第一条信息删除 if (Num == 0) { deleteFirstUSB = Path; } //添加信息 Num++; string num = "信息标号 No." + Num + "\r\n"; string name = "USB名称 " + Name + "\r\n"; string uid = "UID标记 " + sub2 + "\r\n"; string path = "路径信息 " + Path + "\r\n"; //获取当前时间 string time = "log时间 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "\r\n"; //利用文件知识存储后台USB管理信息 string usbFileLog = "usbFileLog.txt"; if (!File.Exists(usbFileLog)) { //创建写入文件 FileStream fs = new FileStream(usbFileLog, FileMode.Create, FileAccess.Write); StreamWriter sw = new StreamWriter(fs); sw.WriteLine(num + name + uid + path + time); sw.Close(); fs.Close(); } else { //写入文件 using (StreamWriter sw = new StreamWriter(usbFileLog, true)) { sw.WriteLine(num + name + uid + path + time); sw.Close(); } } //添加信息 richTextBox1.AppendText(num); richTextBox1.AppendText(name); richTextBox1.AppendText(uid); richTextBox1.AppendText(path + "\r\n"); } } } catch (Exception msg) //异常处理 { MessageBox.Show(msg.Message); } } } //提示 if (Num == 0) { MessageBox.Show(this, "USB使用记录已经删空,无信息!", "提示对话框", MessageBoxButtons.OK, MessageBoxIcon.Warning); label2.Text = "☺☺☺☺☺☺☺\n秀璋提醒您:\nUSB信息已空\n☺☺☺☺☺☺☺"; } else { label2.Text = "☺☺☺☺☺☺☺\n秀璋提醒您:\nUSB信息获取成功\n☺☺☺☺☺☺☺"; } }
//删除USB信息 private void button2_Click(object sender, EventArgs e) { //定义注册表顶级节点 其命名空间是using Microsoft.Win32; RegistryKey USBKey; //检索子项 USBKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Enum\USBSTOR", false); //检索所有子项USBSTOR下的字符串数组 foreach (string sub1 in USBKey.GetSubKeyNames()) { RegistryKey sub1key = USBKey.OpenSubKey(sub1, false); foreach (string sub2 in sub1key.GetSubKeyNames()) { try { //打开sub1key的子项 RegistryKey sub2key = sub1key.OpenSubKey(sub2, false); //检索Service=disk(磁盘)值的子项 cdrom(光盘) if (sub2key.GetValue("Service", "").Equals("disk")) { String Path = "USBSTOR" + "\\" + sub1 + "\\" + sub2; try { //其中sub2存储的是USBSTOR/Disk&Ven_agio&Prod_&Rev_/00A1234567AF&0中 if (deleteFirstUSB == Path) { //获取删除路径信息 string usb = @"SYSTEM\CurrentControlSet\Enum\" + deleteFirstUSB; //usb = @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USBSTOR\Disk&Ven_aigo&Prod_&Rev_\00A1234567AF&0"; //USBSTOR\Disk&Ven_aigo&Prod_&Rev_\00A1234567AF&0 //确认删除 if (MessageBox.Show("确认要删除第一条USB信息?路径:" + usb, "验证提示", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) { //删除文件 //Registry.LocalMachine.DeleteSubKeyTree(usb,true); //设置键值为空(该子项不存在,因此无法删除) //Registry.SetValue(usb, "Service", "null"); //文件存储后台USB删除log信息 string usbDeleteLog = "usbDeleteLog.txt"; if (!File.Exists(usbDeleteLog)) { //创建写入文件 FileStream fs = new FileStream(usbDeleteLog, FileMode.Create, FileAccess.Write); StreamWriter sw = new StreamWriter(fs); sw.WriteLine(Path); sw.Close(); fs.Close(); } else { //写入文件 using (StreamWriter sw = new StreamWriter(usbDeleteLog, true)) { sw.WriteLine(Path); sw.Close(); } } MessageBox.Show("删除USB痕迹成功!路径:" + usb, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); label2.Text = "☺☺☺☺☺☺☺\n秀璋提醒您:\nUSB信息删除成功\n☺☺☺☺☺☺☺"; //清空内容 richTextBox1.Clear(); } } } catch (Exception m) //异常处理 { MessageBox.Show(m.Message); } } } catch (Exception msg) //异常处理 { MessageBox.Show(msg.Message); } } } }