-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUsbStoreProvider.cs
73 lines (56 loc) · 2.54 KB
/
UsbStoreProvider.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
using System;
using System.Collections.Generic;
using System.Management;
using System.Text.RegularExpressions;
namespace WindowsUsb {
class UsbStoreProvider {
private const string QUERY_USB_DISK = "SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'";
private const string QUERY_DISK_TO_PARTITION = "ASSOCIATORS OF {{Win32_DiskDrive.DeviceID='{0}'}} WHERE AssocClass = Win32_DiskDriveToDiskPartition";
private const string QUERY_LOGICAL_DISK_TO_PARTITION = "ASSOCIATORS OF {{Win32_DiskPartition.DeviceID='{0}'}} WHERE AssocClass = Win32_LogicalDiskToPartition";
private const string KEY_DISK_DEVICE_ID = "DeviceID";
private const string KEY_MODEL = "Model";
private const string KEY_PATH = "Name";
private const string KEY_PARTITION_DEVICE_ID = "DeviceID";
private const string KEY_SERIAL_NUMBER = "SerialNumber";
private const string KEY_SIZE = "Size";
private UsbStore buildUsbStore(ManagementObject search){
UsbStore usb = new UsbStore();
usb.setModel(assingModel(search));
usb.setSerialNumber(assingSerialNumber(search));
usb.setSize(assingSize(search));
string diskDeviceId = search[KEY_DISK_DEVICE_ID].ToString();
ManagementObjectSearcher searcher = new ManagementObjectSearcher(String.Format(QUERY_DISK_TO_PARTITION, diskDeviceId));
foreach(ManagementObject searchPartition in searcher.Get()){
assingPartitionInfo(usb, searchPartition);
}
return usb;
}
public UsbStore[] getAllUsbStore(){
List<UsbStore> usbCollection = new List<UsbStore>();
ManagementObjectSearcher searcher = new ManagementObjectSearcher(QUERY_USB_DISK);
foreach(ManagementObject search in searcher.Get()){
usbCollection.Add(buildUsbStore(search));
}
return usbCollection.ToArray();
}
private string assingModel(ManagementObject search){
return search[KEY_MODEL].ToString();
}
private void assingPartitionInfo(UsbStore usb, ManagementObject searchPartition){
string partitionDeviceId = searchPartition[KEY_PARTITION_DEVICE_ID].ToString();
ManagementObjectSearcher searcher = new ManagementObjectSearcher(String.Format(QUERY_LOGICAL_DISK_TO_PARTITION, partitionDeviceId));
foreach(ManagementObject search in searcher.Get()){
usb.setPath(assingPath(search));
}
}
private string assingPath(ManagementObject search){
return search[KEY_PATH].ToString();
}
private string assingSerialNumber(ManagementObject search){
return search[KEY_SERIAL_NUMBER].ToString();
}
private long assingSize(ManagementObject search){
return Convert.ToInt64(search[KEY_SIZE]);
}
}
}