-
Notifications
You must be signed in to change notification settings - Fork 24
/
UsbEventWatcher.cs
865 lines (682 loc) · 35.6 KB
/
UsbEventWatcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
namespace Usb.Events
{
/// <summary>
/// Main Usb.Events class
/// </summary>
public class UsbEventWatcher : IUsbEventWatcher
{
#if DEBUG
public static bool EnableDebugOutput { get; set; }
#endif
#region IUsbEventWatcher
/// <summary>
/// List of USB drive paths
/// </summary>
public List<string> UsbDrivePathList { get; private set; } = new List<string>();
/// <summary>
/// List of USB devices
/// </summary>
public List<UsbDevice> UsbDeviceList { get; private set; } = new List<UsbDevice>();
/// <summary>
/// USB drive mounted event
/// </summary>
public event EventHandler<string>? UsbDriveMounted;
/// <summary>
/// USB drive ejected event
/// </summary>
public event EventHandler<string>? UsbDriveEjected;
/// <summary>
/// USB device added event
/// </summary>
public event EventHandler<UsbDevice>? UsbDeviceAdded;
/// <summary>
/// USB device removed event
/// </summary>
public event EventHandler<UsbDevice>? UsbDeviceRemoved;
#endregion
#region Windows fields
private ManagementEventWatcher? _volumeChangeEventWatcher;
private ManagementEventWatcher? _instanceCreationEventWatcher;
private ManagementEventWatcher? _instanceDeletionEventWatcher;
#endregion
#region Linux and Mac fields
private Task? _watcherTask;
private Task? _mountPointTask;
#endregion
private CancellationTokenSource? _cancellationTokenSource;
private bool _isRunning;
/// <summary>
/// Main Usb.Events class
/// </summary>
/// <param name="startImmediately">Set startImmediately to false if you don't want to start immediately, then call Start()</param>
/// <param name="addAlreadyPresentDevicesToList">Set addAlreadyPresentDevicesToList to true to include already present devices in UsbDeviceList</param>
/// <param name="usePnPEntity">Set usePnPEntity to true to query Win32_PnPEntity instead of Win32_USBControllerDevice in Windows</param>
/// <param name="includeTTY">Set includeTTY to true to monitor the TTY subsystem in Linux (besides the USB subsystem)</param>
public UsbEventWatcher(bool startImmediately = true, bool addAlreadyPresentDevicesToList = false, bool usePnPEntity = false, bool includeTTY = false)
{
if (startImmediately)
{
Start(addAlreadyPresentDevicesToList, usePnPEntity, includeTTY);
}
}
#region Methods
/// <summary>
/// Start monitoring USB events
/// </summary>
/// <param name="addAlreadyPresentDevicesToList">Set addAlreadyPresentDevicesToList to true to include already present devices in UsbDeviceList</param>
/// <param name="usePnPEntity">Set usePnPEntity to true to query Win32_PnPEntity instead of Win32_USBControllerDevice in Windows</param>
/// <param name="includeTTY">Set includeTTY to true to monitor the TTY subsystem in Linux (besides the USB subsystem)</param>
public void Start(bool addAlreadyPresentDevicesToList = false, bool usePnPEntity = false, bool includeTTY = false)
{
if (_isRunning)
return;
_isRunning = true;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
if (addAlreadyPresentDevicesToList)
{
AddAlreadyPresentDevicesToList();
}
StartWindowsWatcher(usePnPEntity);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
_watcherTask = Task.Run(() => StartMacWatcher(InsertedCallback, RemovedCallback));
_cancellationTokenSource = new CancellationTokenSource();
_mountPointTask = Task.Run(async () =>
{
while (!_cancellationTokenSource.Token.IsCancellationRequested)
{
try
{
foreach (UsbDevice usbDevice in UsbDeviceList.Where(device => !string.IsNullOrEmpty(device.DeviceSystemPath)).ToList())
{
GetMacMountPoint(usbDevice.DeviceSystemPath, mountPoint => SetMountPoint(usbDevice, mountPoint));
}
}
catch (InvalidOperationException)
{
// Prevent application crash and ignore possible exception when collection was changed because this may happen by another thread or task
}
await Task.Delay(1000, _cancellationTokenSource.Token);
}
}, _cancellationTokenSource.Token);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
_watcherTask = Task.Run(() => StartLinuxWatcher(InsertedCallback, RemovedCallback, includeTTY));
_cancellationTokenSource = new CancellationTokenSource();
_mountPointTask = Task.Run(async () =>
{
while (!_cancellationTokenSource.Token.IsCancellationRequested)
{
try
{
foreach (UsbDevice usbDevice in UsbDeviceList.Where(device => !string.IsNullOrEmpty(device.DeviceSystemPath)).ToList())
{
GetLinuxMountPoint(usbDevice.DeviceSystemPath, mountPoint => SetMountPoint(usbDevice, mountPoint));
}
}
catch (InvalidOperationException)
{
// Prevent application crash and ignore possible exception when collection was changed because this may happen by another thread or task
}
await Task.Delay(1000, _cancellationTokenSource.Token);
}
}, _cancellationTokenSource.Token);
}
}
private void SetMountPoint(UsbDevice usbDevice, string mountPoint)
{
if (string.IsNullOrEmpty(usbDevice.MountedDirectoryPath) && !string.IsNullOrEmpty(mountPoint))
{
usbDevice.MountedDirectoryPath = mountPoint;
OnDriveInserted(usbDevice.MountedDirectoryPath);
usbDevice.IsEjected = false;
usbDevice.IsMounted = true;
}
else if (!string.IsNullOrEmpty(usbDevice.MountedDirectoryPath) && string.IsNullOrEmpty(mountPoint))
{
OnDriveRemoved(usbDevice.MountedDirectoryPath);
usbDevice.MountedDirectoryPath = mountPoint;
usbDevice.IsEjected = true;
usbDevice.IsMounted = false;
}
}
private void OnDriveInserted(string path)
{
UsbDriveMounted?.Invoke(this, path);
UsbDrivePathList.Add(path);
}
private void OnDriveRemoved(string path)
{
UsbDriveEjected?.Invoke(this, path);
UsbDrivePathList.RemoveAll(p => p == path);
}
private void OnDeviceInserted(UsbDevice usbDevice)
{
UsbDeviceAdded?.Invoke(this, usbDevice);
UsbDeviceList.Add(usbDevice);
}
private void OnDeviceRemoved(UsbDevice usbDevice)
{
UsbDeviceRemoved?.Invoke(this, usbDevice);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
UsbDeviceList.RemoveAll(device => device.DeviceName == usbDevice.DeviceName && device.DeviceSystemPath == usbDevice.DeviceSystemPath);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
UsbDeviceList.RemoveAll(device => device.ProductID == usbDevice.ProductID && device.VendorID == usbDevice.VendorID && device.SerialNumber == usbDevice.SerialNumber);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
UsbDeviceList.RemoveAll(device => device.SerialNumber == usbDevice.SerialNumber);
}
}
#endregion
#region Linux and Mac methods
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Auto)]
delegate void UsbDeviceCallback(UsbDeviceData usbDevice);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Auto)]
delegate void MountPointCallback(string mountPoint);
private void InsertedCallback(UsbDeviceData usbDevice)
{
if (UsbDeviceList.Any(device => device.DeviceName == usbDevice.DeviceName && device.DeviceSystemPath == usbDevice.DeviceSystemPath))
return;
OnDeviceInserted(new UsbDevice(usbDevice));
}
private void RemovedCallback(UsbDeviceData usbDevice)
{
OnDeviceRemoved(new UsbDevice(usbDevice));
}
[DllImport("UsbEventWatcher.Linux.so", CallingConvention = CallingConvention.Cdecl)]
static extern void GetLinuxMountPoint(string syspath, MountPointCallback mountPointCallback);
[DllImport("UsbEventWatcher.Linux.so", CallingConvention = CallingConvention.Cdecl)]
static extern void StartLinuxWatcher(UsbDeviceCallback insertedCallback, UsbDeviceCallback removedCallback, bool includeTTY);
[DllImport("UsbEventWatcher.Linux.so", CallingConvention = CallingConvention.Cdecl)]
static extern void StopLinuxWatcher();
[DllImport("UsbEventWatcher.Mac.dylib", CallingConvention = CallingConvention.Cdecl)]
static extern void GetMacMountPoint(string syspath, MountPointCallback mountPointCallback);
[DllImport("UsbEventWatcher.Mac.dylib", CallingConvention = CallingConvention.Cdecl)]
static extern void StartMacWatcher(UsbDeviceCallback insertedCallback, UsbDeviceCallback removedCallback);
[DllImport("UsbEventWatcher.Mac.dylib", CallingConvention = CallingConvention.Cdecl)]
static extern void StopMacWatcher();
#endregion
/*
Product: DT_101_II
Product Description: DataTraveler 101 II
Product ID: 1625
Serial Number: 0019E06B9C85F9A0F7550C20
Vendor: Kingston
Vendor Description: Kingston Technology
Vendor ID: 0951
Product: DataTraveler_2.0
Product Description: Kingston DataTraveler 102/2.0 / HEMA Flash Drive 2 GB / PNY Attache 4GB Stick
Product ID: 6545
Serial Number: 6CF049E0FBE3B0A069B24172
Vendor: Kingston
Vendor Description: Toshiba Corp.
Vendor ID: 0930
Product: Mass_Storage_Device
Product Description: JetFlash
Product ID: 1000
Serial Number: 08ZLSF32M9ZNUJRJ
Vendor: JetFlash
Vendor Description: Transcend Information, Inc.
Vendor ID: 8564
Product: USB_Flash_Memory
Product Description: Kingston DataTraveler 102/2.0 / HEMA Flash Drive 2 GB / PNY Attache 4GB Stick
Product ID: 6545
Serial Number: 0BB1B8700301387D
Vendor: 0930
Vendor Description: Toshiba Corp.
Vendor ID: 0930
/**/
#region Windows methods
private void AddAlreadyPresentDevicesToList()
{
using ManagementObjectSearcher Win32_USBControllerDevice = new ManagementObjectSearcher($"SELECT * FROM Win32_USBControllerDevice");
foreach (ManagementObject USBControllerDevice in Win32_USBControllerDevice.Get())
{
(string USBControllerDeviceID, string PnPEntityDeviceID) = GetDeviceID(USBControllerDevice);
UsbDevice? usbDevice = GetUsbDevice(PnPEntityDeviceID);
if (usbDevice != null)
{
string diskDriveDeviceID = GetPnPEntityDataAndDiskDriveDeviceID(usbDevice);
GetMountedDirectoryPath(usbDevice, diskDriveDeviceID, USBControllerDeviceID, maxAttempts: 1);
usbDevice.IsEjected = false;
usbDevice.IsMounted = true;
UsbDeviceList.Add(usbDevice);
}
}
}
private void StartWindowsWatcher(bool usePnPEntity = false)
{
UsbDrivePathList = new List<string>(DriveInfo.GetDrives()
.Where(driveInfo => driveInfo.DriveType == DriveType.Removable && driveInfo.IsReady)
.Select(driveInfo => driveInfo.Name.TrimEnd(Path.DirectorySeparatorChar)));
//_deviceChangeEventWatcher = new ManagementEventWatcher();
//_deviceChangeEventWatcher.EventArrived += new EventArrivedEventHandler(DeviceChangeEventWatcher_EventArrived);
//_deviceChangeEventWatcher.Query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2 or EventType = 3"); -licence key detected 3x, no info
//_deviceChangeEventWatcher.Start();
_volumeChangeEventWatcher = new ManagementEventWatcher();
_volumeChangeEventWatcher.EventArrived += new EventArrivedEventHandler(VolumeChangeEventWatcher_EventArrived);
_volumeChangeEventWatcher.Query = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2 or EventType = 3");
_volumeChangeEventWatcher.Start();
_instanceCreationEventWatcher = new ManagementEventWatcher();
_instanceDeletionEventWatcher = new ManagementEventWatcher();
//_instanceCreationEventWatcher.Query = new WqlEventQuery("SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_USBHub'"); - licence key not detected, USB disk detected
//_instanceDeletionEventWatcher.Query = new WqlEventQuery("SELECT * FROM __InstanceDeletionEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_USBHub'"); - licence key not detected, USB disk detected
//_instanceCreationEventWatcher.Query = new WqlEventQuery("SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_USBController'"); - nothing detected
//_instanceDeletionEventWatcher.Query = new WqlEventQuery("SELECT * FROM __InstanceDeletionEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_USBController'"); - nothing detected
if (usePnPEntity)
{
_instanceCreationEventWatcher.EventArrived += new EventArrivedEventHandler(PnPEntityCreationEventWatcher_EventArrived);
_instanceDeletionEventWatcher.EventArrived += new EventArrivedEventHandler(PnPEntityDeletionEventWatcher_EventArrived);
_instanceCreationEventWatcher.Query = new WqlEventQuery("SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_PnPEntity'"); // high CPU load
_instanceDeletionEventWatcher.Query = new WqlEventQuery("SELECT * FROM __InstanceDeletionEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_PnPEntity'"); // high CPU load
}
else
{
_instanceCreationEventWatcher.EventArrived += new EventArrivedEventHandler(USBControllerDeviceCreationEventWatcher_EventArrived);
_instanceDeletionEventWatcher.EventArrived += new EventArrivedEventHandler(USBControllerDeviceDeletionEventWatcher_EventArrived);
_instanceCreationEventWatcher.Query = new WqlEventQuery("SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_USBControllerDevice'"); // -licence key detected 2x, no info
_instanceDeletionEventWatcher.Query = new WqlEventQuery("SELECT * FROM __InstanceDeletionEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_USBControllerDevice'"); // -licence key detected 2x, no info
}
_instanceCreationEventWatcher.Start();
_instanceDeletionEventWatcher.Start();
}
private void VolumeChangeEventWatcher_EventArrived(object sender, EventArrivedEventArgs e)
{
ManagementBaseObject Win32_VolumeChangeEvent = e.NewEvent;
string driveName = Win32_VolumeChangeEvent["DriveName"].ToString();
string eventType = Win32_VolumeChangeEvent["EventType"].ToString();
bool inserted = eventType == "2";
bool removed = eventType == "3";
if (inserted)
{
foreach (UsbDevice usbDevice in UsbDeviceList.Where(device => device.MountedDirectoryPath == driveName))
{
usbDevice.IsEjected = false;
usbDevice.IsMounted = true;
}
OnDriveInserted(driveName);
}
if (removed)
{
foreach (UsbDevice usbDevice in UsbDeviceList.Where(device => device.MountedDirectoryPath == driveName))
{
usbDevice.IsEjected = true;
usbDevice.IsMounted = false;
}
OnDriveRemoved(driveName);
}
}
private static void DebugOutput(ManagementBaseObject managementBaseObject)
{
#if DEBUG
if (!EnableDebugOutput)
return;
System.Diagnostics.Debug.WriteLine(string.Empty);
foreach (PropertyData property in managementBaseObject.Properties)
{
System.Diagnostics.Debug.WriteLine(property.Name + " = " + property.Value);
}
System.Diagnostics.Debug.WriteLine(string.Empty);
#endif
}
private void PnPEntityCreationEventWatcher_EventArrived(object sender, EventArrivedEventArgs e)
{
ManagementBaseObject Win32_PnPEntity = (ManagementBaseObject)e.NewEvent["TargetInstance"];
string PnPEntityDeviceID = Win32_PnPEntity["DeviceID"].ToString();
UsbDevice? usbDevice = GetUsbDevice(PnPEntityDeviceID);
if (usbDevice != null)
{
string diskDriveDeviceID = GetPnPEntityDataAndDiskDriveDeviceID(usbDevice);
GetMountedDirectoryPath(usbDevice, diskDriveDeviceID, USBControllerDeviceID: null, maxAttempts: 1);
usbDevice.IsEjected = false;
usbDevice.IsMounted = true;
OnDeviceInserted(usbDevice);
}
}
private void PnPEntityDeletionEventWatcher_EventArrived(object sender, EventArrivedEventArgs e)
{
ManagementBaseObject Win32_PnPEntity = (ManagementBaseObject)e.NewEvent["TargetInstance"];
string PnPEntityDeviceID = Win32_PnPEntity["DeviceID"].ToString();
UsbDevice? usbDevice = GetUsbDevice(PnPEntityDeviceID);
if (usbDevice != null)
{
string diskDriveDeviceID = GetPnPEntityDataAndDiskDriveDeviceID(usbDevice);
GetMountedDirectoryPath(usbDevice, diskDriveDeviceID, USBControllerDeviceID: null, maxAttempts: 1);
usbDevice.IsEjected = true;
usbDevice.IsMounted = false;
OnDeviceRemoved(usbDevice);
}
}
private void USBControllerDeviceCreationEventWatcher_EventArrived(object sender, EventArrivedEventArgs e)
{
ManagementBaseObject Win32_USBControllerDevice = (ManagementBaseObject)e.NewEvent["TargetInstance"];
(string USBControllerDeviceID, string PnPEntityDeviceID) = GetDeviceID(Win32_USBControllerDevice);
UsbDevice? usbDevice = GetUsbDevice(PnPEntityDeviceID, USBControllerDeviceID);
if (usbDevice != null)
{
usbDevice.IsEjected = false;
usbDevice.IsMounted = true;
OnDeviceInserted(usbDevice);
}
}
private void USBControllerDeviceDeletionEventWatcher_EventArrived(object sender, EventArrivedEventArgs e)
{
ManagementBaseObject Win32_USBControllerDevice = (ManagementBaseObject)e.NewEvent["TargetInstance"];
(string USBControllerDeviceID, string PnPEntityDeviceID) = GetDeviceID(Win32_USBControllerDevice);
UsbDevice? usbDevice = GetUsbDevice(PnPEntityDeviceID, USBControllerDeviceID);
if (usbDevice != null)
{
usbDevice.IsEjected = true;
usbDevice.IsMounted = false;
OnDeviceRemoved(usbDevice);
}
}
private static (string USBControllerDeviceID, string PnPEntityDeviceID) GetDeviceID(ManagementBaseObject Win32_USBControllerDevice)
{
DebugOutput(Win32_USBControllerDevice);
string Win32_USBController = Win32_USBControllerDevice["Antecedent"].ToString();
string[] antecedentInfo = Win32_USBController.Split('"');
string Win32_PnPEntity = Win32_USBControllerDevice["Dependent"].ToString();
string[] dependentInfo = Win32_PnPEntity.Split('"');
if (antecedentInfo.Length < 2 || dependentInfo.Length < 2)
return (string.Empty, string.Empty);
return (antecedentInfo[1].Replace(@"\\", @"\"), dependentInfo[1].Replace(@"\\", @"\"));
}
private static UsbDevice? GetUsbDevice(string PnPEntityDeviceID)
{
string[] deviceInfo = PnPEntityDeviceID.Split('\\');
if (deviceInfo.Length < 3)
return null;
string[] id = deviceInfo[1].Split('&');
if (id.Length < 2)
return null;
if (id[0].Length != 8 || !id[0].StartsWith("VID_"))
return null;
string vendorId = id[0].Substring(4, 4);
if (id[1].Length != 8 || !id[1].StartsWith("PID_"))
return null;
string productId = id[1].Substring(4, 4);
string serial = deviceInfo[2];
UsbDevice usbDevice = new UsbDevice
{
DeviceSystemPath = PnPEntityDeviceID,
ProductID = productId,
SerialNumber = serial,
VendorID = vendorId
};
// TODO::
// get VID, PID from "USB\" and drive letter from "USBSTOR\"
// this doesn't depend on: LIKE '%{serial}%' - in case that "USB\" and "USBSTOR\" don't end with the same serial
// check if Win32_PnPEntity WHERE DeviceID="USB\" and "USBSTOR\" return different results
return usbDevice;
}
private static UsbDevice? GetUsbDevice(string PnPEntityDeviceID, string? USBControllerDeviceID = null)
{
UsbDevice? usbDevice = GetUsbDevice(PnPEntityDeviceID);
if (usbDevice != null)
{
string diskDriveDeviceID = GetPnPEntityDataAndDiskDriveDeviceID(usbDevice);
GetMountedDirectoryPath(usbDevice, diskDriveDeviceID, USBControllerDeviceID);
}
return usbDevice;
}
private static string GetPnPEntityDataAndDiskDriveDeviceID(UsbDevice usbDevice)
{
const string WindowsPortableDevicesClassGuid = "{eec5ad98-8080-425f-922a-dabf3de3f69a}";
#if DEBUG
using ManagementObjectSearcher Win32_PnPEntity = new ManagementObjectSearcher($"SELECT * FROM Win32_PnPEntity WHERE DeviceID LIKE '%{usbDevice.SerialNumber}%'");
#else
using ManagementObjectSearcher Win32_PnPEntity = new ManagementObjectSearcher($"SELECT Caption, ClassGuid, Description, DeviceID, Manufacturer FROM Win32_PnPEntity WHERE DeviceID LIKE '%{usbDevice.SerialNumber}%'");
#endif
bool getPnPEntityData = true;
string diskDriveDeviceID = string.Empty;
foreach (ManagementObject entity in Win32_PnPEntity.Get())
{
DebugOutput(entity);
// https://stackoverflow.com/questions/9525996/how-can-i-detect-whether-a-garmin-gps-device-is-connected-in-mass-storage-mode
using ManagementObjectSearcher Win32_DiskDrive = new ManagementObjectSearcher(
"ASSOCIATORS OF {Win32_PnPEntity.DeviceID=\"" + entity["DeviceID"].ToString().Replace(@"\", @"\\") + "\"} WHERE ResultClass = Win32_DiskDrive");
foreach (ManagementObject diskDrive in Win32_DiskDrive.Get())
{
DebugOutput(diskDrive);
diskDriveDeviceID = diskDrive["DeviceID"].ToString();
}
if (getPnPEntityData)
{
usbDevice.DeviceName = entity["Caption"]?.ToString()?.Trim() ?? string.Empty;
usbDevice.Product = entity["Description"]?.ToString()?.Trim() ?? string.Empty;
usbDevice.ProductDescription = entity["Description"]?.ToString()?.Trim() ?? string.Empty;
usbDevice.Vendor = entity["Manufacturer"]?.ToString()?.Trim() ?? string.Empty;
usbDevice.VendorDescription = entity["Manufacturer"]?.ToString()?.Trim() ?? string.Empty;
}
string ClassGuid = entity["ClassGuid"]?.ToString()?.Trim() ?? string.Empty;
// most USB devices have only one ManagementObject that contains Caption, Description, Manufacturer
// but USB flash drives have several ManagementObject-s and only WindowsPortableDevices has useful info
if (ClassGuid == WindowsPortableDevicesClassGuid)
{
getPnPEntityData = false;
}
/*
Storage Volumes
Class = Volume
ClassGuid = {71a27cdd-812a-11d0-bec7-08002be2092f}
DeviceID = STORAGE\VOLUME\_??_USBSTOR#DISK&VEN_KINGSTON&PROD_DT_101_II&REV_1.00#0019E06B9C85F9A0F7550C20&0#{53F56307-B6BF-11D0-94F2-00A0C91EFB8B}
PNPDeviceID = STORAGE\VOLUME\_??_USBSTOR#DISK&VEN_KINGSTON&PROD_DT_101_II&REV_1.00#0019E06B9C85F9A0F7550C20&0#{53F56307-B6BF-11D0-94F2-00A0C91EFB8B}
Windows Portable Devices (WPD)
Class = WPD
ClassGuid = {eec5ad98-8080-425f-922a-dabf3de3f69a}
Caption = IRM_CCSA_X64FRE_EN-US_DV5
Description = DT 101 II
Manufacturer = Kingston
Name = IRM_CCSA_X64FRE_EN-US_DV5
DeviceID = SWD\WPDBUSENUM\_??_USBSTOR#DISK&VEN_KINGSTON&PROD_DT_101_II&REV_1.00#0019E06B9C85F9A0F7550C20&0#{53F56307-B6BF-11D0-94F2-00A0C91EFB8B}
PNPDeviceID = SWD\WPDBUSENUM\_??_USBSTOR#DISK&VEN_KINGSTON&PROD_DT_101_II&REV_1.00#0019E06B9C85F9A0F7550C20&0#{53F56307-B6BF-11D0-94F2-00A0C91EFB8B}
USB Bus Devices (hubs and host controllers)
Class = USB
ClassGuid = {36fc9e60-c465-11cf-8056-444553540000}
DeviceID = USB\VID_0951&PID_1625\0019E06B9C85F9A0F7550C20
PNPDeviceID = USB\VID_0951&PID_1625\0019E06B9C85F9A0F7550C20
Disk Drives
Class = DiskDrive
ClassGuid = {4d36e967-e325-11ce-bfc1-08002be10318}
Caption = Kingston DT 101 II USB Device
Name = Kingston DT 101 II USB Device
DeviceID = USBSTOR\DISK&VEN_KINGSTON&PROD_DT_101_II&REV_1.00\0019E06B9C85F9A0F7550C20&0
PNPDeviceID = USBSTOR\DISK&VEN_KINGSTON&PROD_DT_101_II&REV_1.00\0019E06B9C85F9A0F7550C20&0
/**/
}
return diskDriveDeviceID;
}
private static void GetMountedDirectoryPath(UsbDevice usbDevice, string diskDriveDeviceID, string? USBControllerDeviceID = null, int maxAttempts = 1000)
{
using ManagementObjectSearcher Win32_USBHub = new ManagementObjectSearcher($"SELECT * FROM Win32_USBHub WHERE DeviceID LIKE '%{usbDevice.SerialNumber}%'");
ManagementObjectCollection USBHubCollection = Win32_USBHub.Get();
if (USBHubCollection.Count > 0)
{
string USBHubDeviceID = string.Empty;
int attempt = 0;
while (++attempt <= maxAttempts)
{
usbDevice.MountedDirectoryPath = GetDevicePath(usbDevice.SerialNumber);
if (string.IsNullOrEmpty(usbDevice.MountedDirectoryPath) && !string.IsNullOrEmpty(diskDriveDeviceID))
{
usbDevice.MountedDirectoryPath = GetDiskDrivePath(diskDriveDeviceID);
}
if (string.IsNullOrEmpty(usbDevice.MountedDirectoryPath) && USBControllerDeviceID != null)
{
// https://stackoverflow.com/questions/20143264/find-windows-drive-letter-of-a-removable-disk-from-usb-vid-pid
if (string.IsNullOrEmpty(USBHubDeviceID))
{
foreach (ManagementObject USBHub in USBHubCollection)
{
USBHubDeviceID = USBHub["DeviceID"].ToString();
}
}
List<string> deviceIDList = new List<string>();
using ManagementObjectSearcher associators = new ManagementObjectSearcher(
"ASSOCIATORS OF {Win32_USBController.DeviceID=\"" + USBControllerDeviceID.Replace(@"\", @"\\") + "\"}");
foreach (ManagementObject associator in associators.Get())
{
foreach (PropertyData propertyData in associator.Properties)
{
if (propertyData.Name == "DeviceID")
{
deviceIDList.Add(associator["DeviceID"].ToString());
break;
}
}
}
// This is working under the assumption that in the list of all devices of Win32_USBController
// Win32_DiskDrive DeviceID="USBSTOR\DISK&VEN_....&PROD_....&REV_....
// comes directly after
// Win32_USBHub DeviceID="USB\\VID_....&PID_....
// in case there are several disk drives
int index = deviceIDList.IndexOf(USBHubDeviceID);
if (index != -1)
{
for (int i = index; i < deviceIDList.Count; ++i)
{
if (deviceIDList[i].Contains("USBSTOR"))
{
using ManagementObjectSearcher Win32_DiskDrive = new ManagementObjectSearcher(
$"SELECT DeviceID FROM Win32_DiskDrive WHERE PNPDeviceID = '{deviceIDList[i].Replace(@"\", @"\\")}'");
foreach (ManagementObject diskDrive in Win32_DiskDrive.Get())
{
usbDevice.MountedDirectoryPath = GetDiskDrivePath(diskDrive["DeviceID"].ToString());
}
break;
}
}
}
}
if (string.IsNullOrEmpty(usbDevice.MountedDirectoryPath))
{
Thread.Sleep(1);
}
else
{
break;
}
}
}
}
private static string GetDevicePath(string serial)
{
string devicePath = string.Empty;
using ManagementObjectSearcher Win32_DiskDrive = new ManagementObjectSearcher(
$"SELECT DeviceID FROM Win32_DiskDrive WHERE PNPDeviceID LIKE '%{serial}%'");
foreach (ManagementObject diskDrive in Win32_DiskDrive.Get())
{
devicePath = GetDiskDrivePath(diskDrive["DeviceID"].ToString());
}
return devicePath;
}
private static string GetDiskDrivePath(string diskDriveDeviceID)
{
string devicePath = string.Empty;
using ManagementObjectSearcher Win32_DiskDriveToDiskPartition = new ManagementObjectSearcher(
"ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + diskDriveDeviceID + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition");
foreach (ManagementObject diskPartition in Win32_DiskDriveToDiskPartition.Get())
{
using ManagementObjectSearcher Win32_LogicalDiskToPartition = new ManagementObjectSearcher(
"ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + diskPartition["DeviceID"] + "'} WHERE AssocClass = Win32_LogicalDiskToPartition");
foreach (ManagementObject logicalDisk in Win32_LogicalDiskToPartition.Get())
{
devicePath = logicalDisk["DeviceID"].ToString();
}
}
return devicePath;
}
#endregion
/// <summary>
/// Dispose of event watchers in Windows
/// Stop native event loop in macOS
/// Stop native event loop in Linux
/// </summary>
public void Dispose()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
_volumeChangeEventWatcher?.Stop();
_volumeChangeEventWatcher?.Dispose();
_volumeChangeEventWatcher = null;
_instanceCreationEventWatcher?.Stop();
_instanceCreationEventWatcher?.Dispose();
_instanceCreationEventWatcher = null;
_instanceDeletionEventWatcher?.Stop();
_instanceDeletionEventWatcher?.Dispose();
_instanceDeletionEventWatcher = null;
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
_cancellationTokenSource?.Cancel();
if (_mountPointTask != null && !_mountPointTask.IsCompleted)
{
try
{
_mountPointTask.GetAwaiter().GetResult();
}
catch
{
}
}
_cancellationTokenSource?.Dispose();
_cancellationTokenSource = null;
StopMacWatcher();
if (_watcherTask != null && !_watcherTask.IsCompleted)
{
try
{
_watcherTask.GetAwaiter().GetResult();
}
catch
{
}
}
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
_cancellationTokenSource?.Cancel();
if (_mountPointTask != null && !_mountPointTask.IsCompleted)
{
try
{
_mountPointTask.GetAwaiter().GetResult();
}
catch
{
}
}
_cancellationTokenSource?.Dispose();
_cancellationTokenSource = null;
StopLinuxWatcher();
if (_watcherTask != null && !_watcherTask.IsCompleted)
{
try
{
_watcherTask.GetAwaiter().GetResult();
}
catch
{
}
}
}
_isRunning = false;
}
}
}