Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Windows.Forms;
- using System.Management;
- using System.Diagnostics;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<String> prec = query();
- while (true)
- {
- System.Threading.Thread.Sleep(1000);
- List<String> succ = query();
- if (succ.Count > prec.Count)
- {
- checknexecute(succ, prec);
- }
- prec = null;
- prec = succ;
- succ = null;
- GC.Collect();
- }
- }
- static List<String> query()
- {
- List<String> lsusb = new List<string>();
- ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT deviceid FROM cim_logicaldevice");
- ManagementObjectCollection usb = searcher.Get();
- foreach (ManagementObject obj in usb)
- {
- lsusb.Add(obj.Properties["deviceid"].Value.ToString());
- }
- searcher.Dispose();
- usb.Dispose();
- GC.Collect();
- return lsusb;
- }
- static void checknexecute(List<String> succ, List<String> prec)
- {
- foreach (String usbid in succ)
- {
- if (!prec.Contains(usbid))
- {
- File.AppendAllText("log.log", "PLUGGED " + DateTime.Now.ToString() + " --- " + usbid + "\r\n");
- }
- }
- File.AppendAllText("log.log", "\r\n");
- succ = null;
- prec = null;
- GC.Collect();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement