-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiskConfig.cs
180 lines (158 loc) · 5.47 KB
/
DiskConfig.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
namespace powerLabel
{
public class DiskConfig
{
public DiskConfig()
{
}
public DiskConfig(ComputerSystem computerSystem)
{
disk = new Disk();
this.computerSystem = computerSystem;
}
public DiskConfig(Disk disk, ComputerSystem computerSystem, int busType)
{
this.disk = disk;
this.computerSystem = computerSystem;
this.busType = busTypeEncoding[busType];
}
public int id { get; set; }
public Disk disk { get; set; }
public ComputerSystem computerSystem { get; set; }
public bool systemDisk { get; set; }
public string busType { get; set; }
private static string[] busTypeEncoding = {
"Unknown",
"SCSI",
"ATAPI",
"ATA",
"IEEE 1394",
"SSA",
"Fibre Channel",
"USB",
"RAID",
"iSCSI",
"SAS",
"SATA",
"SD",
"MMC",
"MAX",
"File Backed Virtual",
"Storage Spaces",
"NVMe"
};
public static List<DiskConfig> GetDisks(ComputerSystem system)
{
List<DiskConfig> list = new List<DiskConfig>();
uint osDiskId = 255;
ManagementObjectCollection partitions = PSInterface.RunObjectQuery("SELECT * FROM Win32_DiskPartition");
foreach (ManagementObject item in partitions)
{
if ((bool)item["BootPartition"])
{
osDiskId = (uint)item["DiskIndex"];
}
}
ManagementScope scope = new ManagementScope("root\\Microsoft\\Windows\\Storage");
scope.Connect();
ObjectQuery query = new ObjectQuery("SELECT * FROM MSFT_PhysicalDisk");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection returned = searcher.Get();
foreach (ManagementObject item in returned)
{
DiskConfig diskConfig = new DiskConfig(system);
diskConfig.disk.model = (string)item["Model"];
diskConfig.disk.size = (ulong)item["Size"];
diskConfig.busType = busTypeEncoding[(UInt16)item["busType"]];
switch ((ushort)item["MediaType"])
{
case 0:
diskConfig.disk.mediaType = "Unspecified";
break;
case 3:
diskConfig.disk.mediaType = "HDD";
break;
case 4:
diskConfig.disk.mediaType = "SSD";
break;
case 5:
diskConfig.disk.mediaType = "SCM";
break;
default:
break;
}
diskConfig.disk.serialNumber = (string)item["SerialNumber"];
if (UInt32.Parse((string)item["DeviceId"]) == osDiskId)
{
diskConfig.systemDisk = true;
}
if (diskConfig.busType == "USB")
{
continue;
}
list.Add(diskConfig);
}
return list.OrderByDescending(disk => disk.systemDisk).ThenByDescending(disk => disk.disk.mediaType).ThenBy(disk => disk.disk.size).ToList();
}
public override string ToString()
{
ulong shortSize = disk.size / 1000000000;
string unit;
string os = "";
if (shortSize.ToString().Length == 3)
{
unit = "GB";
}
else
{
unit = "TB";
shortSize = shortSize / 1000;
}
if (systemDisk)
{
if (computerSystem.operatingSystem.caption.Contains("10"))
{
os = "+ W10P";
}
else if (computerSystem.operatingSystem.caption.Contains("11"))
{
os = "+ W11P";
}
if (computerSystem.operatingSystem.language != "NL")
{
os += " " + computerSystem.operatingSystem.language;
}
}
if (disk.mediaType == "SSD")
{
if (busType == "NVMe")
{
return $"{shortSize}{unit} {busType} {os} ";
}
return $"{shortSize}{unit} {busType} {disk.mediaType} {os} ";
}
if (disk.mediaType == "HDD")
{
return $"{shortSize}{unit} {disk.mediaType} {os} ";
}
return $"{shortSize}{unit} {disk.mediaType} {os} ";
}
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType()) return false;
DiskConfig diskConfig = (DiskConfig)obj;
if (this.disk.Equals(diskConfig.disk) &&
this.busType.Trim() == diskConfig.busType.Trim() &&
this.systemDisk == diskConfig.systemDisk
)
{
return true;
}
return false;
}
}
}