-
Notifications
You must be signed in to change notification settings - Fork 118
/
WmiSystemDriveSerialNumberDeviceIdComponent.cs
44 lines (38 loc) · 1.5 KB
/
WmiSystemDriveSerialNumberDeviceIdComponent.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
using System;
using System.Linq;
using System.Management;
namespace DeviceId.Windows.Wmi.Components;
/// <summary>
/// An implementation of <see cref="IDeviceIdComponent"/> that uses the system drive's serial number.
/// </summary>
public class WmiSystemDriveSerialNumberDeviceIdComponent : IDeviceIdComponent
{
/// <summary>
/// Initializes a new instance of the <see cref="WmiSystemDriveSerialNumberDeviceIdComponent"/> class.
/// </summary>
public WmiSystemDriveSerialNumberDeviceIdComponent() { }
/// <summary>
/// Gets the component value.
/// </summary>
/// <returns>The component value.</returns>
public string GetValue()
{
var systemLogicalDiskDeviceId = Environment.GetFolderPath(Environment.SpecialFolder.System).Substring(0, 2);
var queryString = $"SELECT * FROM Win32_LogicalDisk where DeviceId = '{systemLogicalDiskDeviceId}'";
using var searcher = new ManagementObjectSearcher(queryString);
foreach (var disk in searcher.Get().OfType<ManagementObject>())
{
foreach (var partition in disk.GetRelated("Win32_DiskPartition").OfType<ManagementObject>())
{
foreach (var drive in partition.GetRelated("Win32_DiskDrive").OfType<ManagementObject>())
{
if (drive["SerialNumber"] is string serialNumber)
{
return serialNumber;
}
}
}
}
return null;
}
}