-
Notifications
You must be signed in to change notification settings - Fork 2
/
USBDevice.cs
107 lines (95 loc) · 3.09 KB
/
USBDevice.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RegistryReader
{
public class USBDevice
{
private const string DATE_FORMAT = "dd-MMM-yy h:mm:ss tt zzz";
public string Serial { get; private set; }
public string FriendlyName { get; set; }
public DateTime FirstInsertion { get; private set; }
public DateTime LastInsertion { get; private set; }
public DateTime LastRemoval { get; private set; }
public string Vendor { get; private set; }
public string Product { get; private set; }
public string DriveLetter { get; private set; }
public string DeviceGUID { get; private set; }
public void SetFirstInsertion(string firstInsertion)
{
FirstInsertion = ConvertToLocalTime(firstInsertion);
}
public void SetLastInsertion(string lastInsertion)
{
LastInsertion = ConvertToLocalTime(lastInsertion);
}
public void SetLastRemoval(string lastRemoval)
{
LastRemoval = ConvertToLocalTime(lastRemoval);
}
public void SetDescription(string description)
{
if(description != null)
{
string[] split = description.Split('&');
if(split.Length > 1)
{
Vendor = split[1].Split('_')[1];
Product = split[2].Split('_')[1];
}
}
}
public void SetSerial(string serial)
{
if(serial != null)
{
if (!serial.StartsWith("6&"))
{
string[] split = serial.Split('&');
if(split.Length > 1)
{
Serial = split[split.Length - 2];
}
}
else
{
Serial = serial;
}
}
}
public void SetDriveLetter(string driveString)
{
if(driveString != null)
{
var split = driveString.Split('\\');
if(split.Length > 1)
{
DriveLetter = split[split.Length - 1];
}
}
}
public void SetDeviceGUID(string guidString)
{
if(guidString != null)
{
var split = guidString.Split('{');
if(split.Length > 1)
{
DeviceGUID = split[1].TrimEnd('}');
}
}
}
/// <summary>
/// Converts UTC time stored in the registry to local time using DateTime.ParseExact
/// </summary>
/// <param name="utcTime"></param>
/// <returns></returns>
private DateTime ConvertToLocalTime(string utcTime)
{
return utcTime == null ? DateTime.MinValue : DateTime.ParseExact(utcTime, DATE_FORMAT, CultureInfo.InvariantCulture);
}
}
}