Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Management;
- using USBLib;
- using System.Threading;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- using System.Text;
- namespace usbdetecter
- {
- class Program
- {
- [DllImport("kernel32.dll")]
- static extern IntPtr GetConsoleWindow();
- [DllImport("user32.dll")]
- static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
- const int SW_HIDE = 0;
- const int SW_SHOW = 5;
- static public List<string> check = new List<string>();
- static public List<string> now = new List<string>();
- static public void prepare()
- {
- foreach (USB.USBController host in USB.GetHostControllers())
- {
- foreach (USB.USBPort port in host.GetRootHub().GetPorts())
- {
- if (port.IsDeviceConnected && (port.HubDevicePath.Contains("ROOT_HUB20") || port.HubDevicePath.Contains("ROOT_HUB30")))
- {
- USB.USBDevice device = port.GetDevice();
- check.Add(device.InstanceID);
- }
- }
- }
- }
- static void Main(string[] args)
- {
- var handle = GetConsoleWindow();
- // Hide
- ShowWindow(handle, SW_HIDE);
- // Show
- //ShowWindow(handle, SW_SHOW);
- prepare();
- using (var control = new USBControl())
- {
- Console.ReadLine();
- }
- Console.Read();
- }
- class USBControl : IDisposable
- {
- // used for monitoring plugging and unplugging of USB devices.
- private ManagementEventWatcher watcherAttach;
- private ManagementEventWatcher watcherDetach;
- public USBControl()
- {
- // Add USB plugged event watching
- watcherAttach = new ManagementEventWatcher();
- watcherAttach.EventArrived += Attaching;
- watcherAttach.Query = new WqlEventQuery("SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_USBHub' or TargetInstance ISA 'Win32_USBController' or TargetInstance ISA 'Win32_USBControllerDevice'");
- watcherAttach.Start();
- // Add USB unplugged event watching
- watcherDetach = new ManagementEventWatcher();
- watcherDetach.EventArrived += Detaching;
- watcherDetach.Query = new WqlEventQuery("SELECT * FROM __InstanceDeletionEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_USBHub' or TargetInstance ISA 'Win32_USBController' or TargetInstance ISA 'Win32_USBControllerDevice'");
- watcherDetach.Start();
- }
- public void Dispose()
- {
- watcherAttach.Stop();
- watcherDetach.Stop();
- Thread.Sleep(1000);
- //you may want to yield or Thread.Sleep
- watcherAttach.Dispose();
- watcherDetach.Dispose();
- Thread.Sleep(1000);
- //you may want to yield or Thread.Sleep
- }
- void Attaching(object sender, EventArrivedEventArgs e)
- {
- if (sender != watcherAttach) return;
- Console.WriteLine("Attaching");
- insert();
- }
- void Detaching(object sender, EventArrivedEventArgs e)
- {
- if (sender != watcherDetach) return;
- Console.WriteLine("Detaching");
- remove();
- }
- ~USBControl()
- {
- this.Dispose();// for ease of readability I left out the complete Dispose pattern
- }
- }
- //---------------------------update
- static public void update()
- {
- now.Clear();
- foreach (USB.USBController host in USB.GetHostControllers())
- {
- foreach (USB.USBPort port in host.GetRootHub().GetPorts())
- {
- if (port.IsDeviceConnected && (port.HubDevicePath.Contains("ROOT_HUB20") || port.HubDevicePath.Contains("ROOT_HUB30")))
- {
- USB.USBDevice device = port.GetDevice();
- now.Add(device.InstanceID);
- }
- }
- }
- }
- //-----------------------------insert
- static public void insert()
- {
- update();
- foreach (string y in now)
- {
- if (check.Contains(y) == false)
- {
- USB.USBDevice x = USB.FindDeviceByInstanceID(y);
- string tmp = "";
- tmp += "HubDevicePath=" + x.HubDevicePath + "\r\n";
- tmp += "PortNumber=" + x.PortNumber + "\r\n";
- tmp += "DriverKey=" + x.DriverKey + "\r\n";
- tmp += "InstanceID=" + x.InstanceID + "\r\n";
- tmp += "Name=" + x.Name + "\r\n";
- tmp += "Manufacturer=" + x.Manufacturer + "\r\n";
- tmp += "Product=" + x.Product + "\r\n";
- tmp += "SerialNumber=" + x.SerialNumber + "\r\n";
- tmp += "time:" + DateTime.Now + "\r\n\r\n\r\n";
- write(tmp);
- check.Add(x.InstanceID);
- }
- }
- }
- //-------------------out
- static public void remove()
- {
- update();
- string getout="";
- foreach (string y in check)
- {
- if (now.Contains(y) == false)
- {
- string tmp = "";
- tmp += "remove\r\n";
- tmp += y + "time:" + DateTime.Now + "\r\n\r\n\r\n";
- write(tmp);
- getout = y;
- }
- }
- check.Remove(getout);
- }
- static public void write(string insert)
- {
- StreamWriter sw = new StreamWriter(@"d:\LOG.txt", true);
- sw.WriteLine(insert);
- sw.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement