Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Management;
  5. using USBLib;
  6. using System.Threading;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using System.Windows.Forms;
  10. using System.Text;
  11.  
  12. namespace usbdetecter
  13. {
  14.  
  15. class Program
  16. {
  17. [DllImport("kernel32.dll")]
  18. static extern IntPtr GetConsoleWindow();
  19.  
  20. [DllImport("user32.dll")]
  21. static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  22.  
  23. const int SW_HIDE = 0;
  24. const int SW_SHOW = 5;
  25.  
  26. static public List<string> check = new List<string>();
  27. static public List<string> now = new List<string>();
  28.  
  29. static public void prepare()
  30. {
  31. foreach (USB.USBController host in USB.GetHostControllers())
  32. {
  33. foreach (USB.USBPort port in host.GetRootHub().GetPorts())
  34. {
  35. if (port.IsDeviceConnected && (port.HubDevicePath.Contains("ROOT_HUB20") || port.HubDevicePath.Contains("ROOT_HUB30")))
  36. {
  37. USB.USBDevice device = port.GetDevice();
  38. check.Add(device.InstanceID);
  39.  
  40. }
  41. }
  42. }
  43. }
  44.  
  45. static void Main(string[] args)
  46. {
  47.  
  48. var handle = GetConsoleWindow();
  49.  
  50. // Hide
  51. ShowWindow(handle, SW_HIDE);
  52.  
  53. // Show
  54. //ShowWindow(handle, SW_SHOW);
  55.  
  56.  
  57. prepare();
  58.  
  59. using (var control = new USBControl())
  60. {
  61. Console.ReadLine();
  62. }
  63.  
  64. Console.Read();
  65. }
  66.  
  67. class USBControl : IDisposable
  68. {
  69. // used for monitoring plugging and unplugging of USB devices.
  70. private ManagementEventWatcher watcherAttach;
  71. private ManagementEventWatcher watcherDetach;
  72.  
  73. public USBControl()
  74. {
  75. // Add USB plugged event watching
  76. watcherAttach = new ManagementEventWatcher();
  77. watcherAttach.EventArrived += Attaching;
  78. watcherAttach.Query = new WqlEventQuery("SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_USBHub' or TargetInstance ISA 'Win32_USBController' or TargetInstance ISA 'Win32_USBControllerDevice'");
  79. watcherAttach.Start();
  80. // Add USB unplugged event watching
  81. watcherDetach = new ManagementEventWatcher();
  82. watcherDetach.EventArrived += Detaching;
  83. watcherDetach.Query = new WqlEventQuery("SELECT * FROM __InstanceDeletionEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_USBHub' or TargetInstance ISA 'Win32_USBController' or TargetInstance ISA 'Win32_USBControllerDevice'");
  84. watcherDetach.Start();
  85.  
  86. }
  87.  
  88. public void Dispose()
  89. {
  90. watcherAttach.Stop();
  91. watcherDetach.Stop();
  92. Thread.Sleep(1000);
  93. //you may want to yield or Thread.Sleep
  94. watcherAttach.Dispose();
  95. watcherDetach.Dispose();
  96. Thread.Sleep(1000);
  97. //you may want to yield or Thread.Sleep
  98. }
  99.  
  100. void Attaching(object sender, EventArrivedEventArgs e)
  101. {
  102. if (sender != watcherAttach) return;
  103. Console.WriteLine("Attaching");
  104. insert();
  105. }
  106.  
  107. void Detaching(object sender, EventArrivedEventArgs e)
  108. {
  109. if (sender != watcherDetach) return;
  110. Console.WriteLine("Detaching");
  111. remove();
  112. }
  113.  
  114. ~USBControl()
  115. {
  116. this.Dispose();// for ease of readability I left out the complete Dispose pattern
  117. }
  118. }
  119. //---------------------------update
  120.  
  121. static public void update()
  122. {
  123. now.Clear();
  124. foreach (USB.USBController host in USB.GetHostControllers())
  125. {
  126. foreach (USB.USBPort port in host.GetRootHub().GetPorts())
  127. {
  128. if (port.IsDeviceConnected && (port.HubDevicePath.Contains("ROOT_HUB20") || port.HubDevicePath.Contains("ROOT_HUB30")))
  129. {
  130. USB.USBDevice device = port.GetDevice();
  131. now.Add(device.InstanceID);
  132. }
  133. }
  134. }
  135. }
  136.  
  137. //-----------------------------insert
  138.  
  139. static public void insert()
  140. {
  141. update();
  142.  
  143. foreach (string y in now)
  144. {
  145. if (check.Contains(y) == false)
  146. {
  147. USB.USBDevice x = USB.FindDeviceByInstanceID(y);
  148. string tmp = "";
  149. tmp += "HubDevicePath=" + x.HubDevicePath + "\r\n";
  150. tmp += "PortNumber=" + x.PortNumber + "\r\n";
  151. tmp += "DriverKey=" + x.DriverKey + "\r\n";
  152. tmp += "InstanceID=" + x.InstanceID + "\r\n";
  153. tmp += "Name=" + x.Name + "\r\n";
  154. tmp += "Manufacturer=" + x.Manufacturer + "\r\n";
  155. tmp += "Product=" + x.Product + "\r\n";
  156. tmp += "SerialNumber=" + x.SerialNumber + "\r\n";
  157. tmp += "time:" + DateTime.Now + "\r\n\r\n\r\n";
  158.  
  159. write(tmp);
  160.  
  161. check.Add(x.InstanceID);
  162. }
  163. }
  164.  
  165.  
  166. }
  167.  
  168. //-------------------out
  169.  
  170. static public void remove()
  171. {
  172. update();
  173. string getout="";
  174. foreach (string y in check)
  175. {
  176. if (now.Contains(y) == false)
  177. {
  178. string tmp = "";
  179. tmp += "remove\r\n";
  180. tmp += y + "time:" + DateTime.Now + "\r\n\r\n\r\n";
  181. write(tmp);
  182. getout = y;
  183. }
  184. }
  185. check.Remove(getout);
  186. }
  187.  
  188. static public void write(string insert)
  189. {
  190. StreamWriter sw = new StreamWriter(@"d:\LOG.txt", true);
  191. sw.WriteLine(insert);
  192. sw.Close();
  193. }
  194. }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement