Contents

Tell whether SSD or not in C#介绍了怎么用C#来判断一个磁盘是不是SSD固态硬盘。文中使用的方法来源于微软一篇介绍Windows7磁盘碎片整理的文章Windows 7 Disk Defragmenter User Interface Overview,在这篇文章中指出Windows7在做磁盘碎片整理的时候会把SSD的磁盘排除在外,它用了3种检测方法(如果前面一种方法失败了会尝试下面一种方法):

主要就是用Windows的CreateFile.aspx)API来打开磁盘设备,然后调用DeviceIoControl.aspx)API来获取这些信息。

详见下面的代码,除了判断是不是SSD,也顺带用Management.aspx)获取机器的硬件信息,包括:机器名,MAC地址,序列号,Model,Manufacturer,本地用户等信息。

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

static void Main(string[] args)
{
GetPhysicalDriveNumberFromLogicalDriveLetter();

//CheckSSDForPhysicalDrive();

//string version = "1.0";
//string computername = "UNKNOWN";
//string servicetag = "UNKNOWN";
//string wifimac = "UNKNOWN";
//string model = "UNKNOWN";
//string manufacturer = "UNKNOWN";

//// Get Manufacturer
//manufacturer = GetManufacturer();
//// Get Model
//model = GetModel();
//// Get computer name
//computername = GetComputerName();
//// Get the wifi MAC address
//wifimac = GetWifiMAC();
//// Get the service tag
//servicetag = GetServiceTag();
//// Write header to console
//Console.WriteLine("*************************************");
//Console.WriteLine("ASSET TAGGING VERSION {0}", version);
//Console.WriteLine("*************************************");
//// Write info to console
//Console.WriteLine("Make: {0}", manufacturer);
//Console.WriteLine("Model: {0}", model);
//Console.WriteLine("Computer Name: {0}", computername);
//Console.WriteLine("Wireless MAC: {0}", wifimac);
//Console.WriteLine("Service Tag: {0}", servicetag);
//// Wait for user to press a key
//Console.WriteLine();
//Console.WriteLine("Press any key to exit");
//Console.ReadKey();
}

private static void CheckSSDForPhysicalDrive()
{
Console.WriteLine("Input physical drive number:");
string sDrive = "\\\\.\\PhysicalDrive" + Console.ReadLine();

Console.WriteLine(
"Select method (0: No seek penalty, 1: Nominal media rotation rate):");
switch (int.Parse(Console.ReadLine()))
{
case 0:
HasNoSeekPenalty(sDrive);
break;

case 1:
HasNominalMediaRotationRate(sDrive);
break;
}

Console.ReadLine();
}

private static string GetComputerName()
{
return Environment.MachineName;
}

private static string GetWifiMAC()
{
string MAC = "UNKNOWN";

foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
{
MAC = nic.GetPhysicalAddress().ToString();
// Insert hyphens
MAC = String.Join("-", Regex.Matches(MAC, @"\w{2}").Cast<Match>());
}
}
return MAC;
}

private static string GetServiceTag()
{
string servicetag = "UNKNOWN";

ManagementClass wmi = new ManagementClass("Win32_Bios");
foreach (ManagementObject bios in wmi.GetInstances())
{
servicetag = bios.Properties["Serialnumber"].Value.ToString().Trim();
}
return servicetag;
}

private static string GetModel()
{
string model = "UNKNOWN";

ManagementClass wmi = new ManagementClass("Win32_ComputerSystem");
foreach (ManagementObject computer in wmi.GetInstances())
{
model = computer.Properties["Model"].Value.ToString().Trim();
}
return model;
}

private static string GetManufacturer()
{
string manufacturer = "UNKNOWN";

ManagementClass wmi = new ManagementClass("Win32_ComputerSystem");
foreach (ManagementObject computer in wmi.GetInstances())
{
manufacturer = computer.Properties["Manufacturer"].Value.ToString().Trim();
}
return manufacturer;
}

private static string GetLocalUser()
{
string localUser = "UNKNOWN";

SelectQuery query = new SelectQuery("Select * from Win32_UserAccount Where LocalAccount=True");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject envVar in searcher.Get())
{
localUser += envVar["Name"] + ",";
}
return localUser;
}

// For CreateFile to get handle to drive
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const uint FILE_SHARE_READ = 0x00000001;
private const uint FILE_SHARE_WRITE = 0x00000002;
private const uint OPEN_EXISTING = 3;
private const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;

// CreateFile to get handle to drive
[DllImport("kernel32.dll", SetLastError = true)]
private static extern SafeFileHandle CreateFileW(
[MarshalAs(UnmanagedType.LPWStr)]
string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);

// For control codes
private const uint FILE_DEVICE_MASS_STORAGE = 0x0000002d;
private const uint IOCTL_STORAGE_BASE = FILE_DEVICE_MASS_STORAGE;
private const uint FILE_DEVICE_CONTROLLER = 0x00000004;
private const uint IOCTL_SCSI_BASE = FILE_DEVICE_CONTROLLER;
private const uint METHOD_BUFFERED = 0;
private const uint FILE_ANY_ACCESS = 0;
private const uint FILE_READ_ACCESS = 0x00000001;
private const uint FILE_WRITE_ACCESS = 0x00000002;
private const uint IOCTL_VOLUME_BASE = 0x00000056;

private static uint CTL_CODE(uint DeviceType, uint Function,
uint Method, uint Access)
{
return ((DeviceType << 16) | (Access << 14) |
(Function << 2) | Method);
}

// For DeviceIoControl to check no seek penalty
private const uint StorageDeviceSeekPenaltyProperty = 7;
private const uint PropertyStandardQuery = 0;

[StructLayout(LayoutKind.Sequential)]
private struct STORAGE_PROPERTY_QUERY
{
public uint PropertyId;
public uint QueryType;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public byte[] AdditionalParameters;
}

[StructLayout(LayoutKind.Sequential)]
private struct DEVICE_SEEK_PENALTY_DESCRIPTOR
{
public uint Version;
public uint Size;
[MarshalAs(UnmanagedType.U1)]
public bool IncursSeekPenalty;
}

// DeviceIoControl to check no seek penalty
[DllImport("kernel32.dll", EntryPoint = "DeviceIoControl",
SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DeviceIoControl(
SafeFileHandle hDevice,
uint dwIoControlCode,
ref STORAGE_PROPERTY_QUERY lpInBuffer,
uint nInBufferSize,
ref DEVICE_SEEK_PENALTY_DESCRIPTOR lpOutBuffer,
uint nOutBufferSize,
out uint lpBytesReturned,
IntPtr lpOverlapped);

// For DeviceIoControl to check nominal media rotation rate
private const uint ATA_FLAGS_DATA_IN = 0x02;

[StructLayout(LayoutKind.Sequential)]
private struct ATA_PASS_THROUGH_EX
{
public ushort Length;
public ushort AtaFlags;
public byte PathId;
public byte TargetId;
public byte Lun;
public byte ReservedAsUchar;
public uint DataTransferLength;
public uint TimeOutValue;
public uint ReservedAsUlong;
public IntPtr DataBufferOffset;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] PreviousTaskFile;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] CurrentTaskFile;
}

[StructLayout(LayoutKind.Sequential)]
private struct ATAIdentifyDeviceQuery
{
public ATA_PASS_THROUGH_EX header;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public ushort[] data;
}

// DeviceIoControl to check nominal media rotation rate
[DllImport("kernel32.dll", EntryPoint = "DeviceIoControl",
SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DeviceIoControl(
SafeFileHandle hDevice,
uint dwIoControlCode,
ref ATAIdentifyDeviceQuery lpInBuffer,
uint nInBufferSize,
ref ATAIdentifyDeviceQuery lpOutBuffer,
uint nOutBufferSize,
out uint lpBytesReturned,
IntPtr lpOverlapped);

// For error message
private const uint FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;

[DllImport("kernel32.dll", SetLastError = true)]
static extern uint FormatMessage(
uint dwFlags,
IntPtr lpSource,
uint dwMessageId,
uint dwLanguageId,
StringBuilder lpBuffer,
uint nSize,
IntPtr Arguments);


// Method for no seek penalty
private static void HasNoSeekPenalty(string sDrive)
{
SafeFileHandle hDrive = CreateFileW(
sDrive,
0, // No access to drive
FILE_SHARE_READ | FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
IntPtr.Zero);

if (hDrive == null || hDrive.IsInvalid)
{
string message = GetErrorMessage(Marshal.GetLastWin32Error());
Console.WriteLine("CreateFile failed. " + message);
}

uint IOCTL_STORAGE_QUERY_PROPERTY = CTL_CODE(
IOCTL_STORAGE_BASE, 0x500,
METHOD_BUFFERED, FILE_ANY_ACCESS); // From winioctl.h

STORAGE_PROPERTY_QUERY query_seek_penalty =
new STORAGE_PROPERTY_QUERY();
query_seek_penalty.PropertyId = StorageDeviceSeekPenaltyProperty;
query_seek_penalty.QueryType = PropertyStandardQuery;

DEVICE_SEEK_PENALTY_DESCRIPTOR query_seek_penalty_desc =
new DEVICE_SEEK_PENALTY_DESCRIPTOR();

uint returned_query_seek_penalty_size;

bool query_seek_penalty_result = DeviceIoControl(
hDrive,
IOCTL_STORAGE_QUERY_PROPERTY,
ref query_seek_penalty,
(uint)Marshal.SizeOf(query_seek_penalty),
ref query_seek_penalty_desc,
(uint)Marshal.SizeOf(query_seek_penalty_desc),
out returned_query_seek_penalty_size,
IntPtr.Zero);

hDrive.Close();

if (query_seek_penalty_result == false)
{
string message = GetErrorMessage(Marshal.GetLastWin32Error());
Console.WriteLine("DeviceIoControl failed. " + message);
}
else
{
if (query_seek_penalty_desc.IncursSeekPenalty == false)
{
Console.WriteLine("This drive has NO SEEK penalty.");
}
else
{
Console.WriteLine("This drive has SEEK penalty.");
}
}
}

// Method for nominal media rotation rate
// (Administrative privilege is required)
private static void HasNominalMediaRotationRate(string sDrive)
{
SafeFileHandle hDrive = CreateFileW(
sDrive,
GENERIC_READ | GENERIC_WRITE, // Administrative privilege is required
FILE_SHARE_READ | FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
IntPtr.Zero);

if (hDrive == null || hDrive.IsInvalid)
{
string message = GetErrorMessage(Marshal.GetLastWin32Error());
Console.WriteLine("CreateFile failed. " + message);
}

uint IOCTL_ATA_PASS_THROUGH = CTL_CODE(
IOCTL_SCSI_BASE, 0x040b, METHOD_BUFFERED,
FILE_READ_ACCESS | FILE_WRITE_ACCESS); // From ntddscsi.h

ATAIdentifyDeviceQuery id_query = new ATAIdentifyDeviceQuery();
id_query.data = new ushort[256];

id_query.header.Length = (ushort)Marshal.SizeOf(id_query.header);
id_query.header.AtaFlags = (ushort)ATA_FLAGS_DATA_IN;
id_query.header.DataTransferLength =
(uint)(id_query.data.Length * 2); // Size of "data" in bytes
id_query.header.TimeOutValue = 3; // Sec
id_query.header.DataBufferOffset = (IntPtr)Marshal.OffsetOf(
typeof(ATAIdentifyDeviceQuery), "data");
id_query.header.PreviousTaskFile = new byte[8];
id_query.header.CurrentTaskFile = new byte[8];
id_query.header.CurrentTaskFile[6] = 0xec; // ATA IDENTIFY DEVICE

uint retval_size;

bool result = DeviceIoControl(
hDrive,
IOCTL_ATA_PASS_THROUGH,
ref id_query,
(uint)Marshal.SizeOf(id_query),
ref id_query,
(uint)Marshal.SizeOf(id_query),
out retval_size,
IntPtr.Zero);

hDrive.Close();

if (result == false)
{
string message = GetErrorMessage(Marshal.GetLastWin32Error());
Console.WriteLine("DeviceIoControl failed. " + message);
}
else
{
// Word index of nominal media rotation rate
// (1 means non-rotate device)
const int kNominalMediaRotRateWordIndex = 217;

if (id_query.data[kNominalMediaRotRateWordIndex] == 1)
{
Console.WriteLine("This drive is NON-ROTATE device.");
}
else
{
Console.WriteLine("This drive is ROTATE device.");
}
}
}

// Method for error message
private static string GetErrorMessage(int code)
{
StringBuilder message = new StringBuilder(255);

FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM,
IntPtr.Zero,
(uint)code,
0,
message,
(uint)message.Capacity,
IntPtr.Zero);

return message.ToString();
}


// For DeviceIoControl to get disk extents
[StructLayout(LayoutKind.Sequential)]
private struct DISK_EXTENT
{
public uint DiskNumber;
public long StartingOffset;
public long ExtentLength;
}

[StructLayout(LayoutKind.Sequential)]
private struct VOLUME_DISK_EXTENTS
{
public uint NumberOfDiskExtents;
[MarshalAs(UnmanagedType.ByValArray)]
public DISK_EXTENT[] Extents;
}

// DeviceIoControl to get disk extents
[DllImport("kernel32.dll", EntryPoint = "DeviceIoControl",
SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DeviceIoControl(
SafeFileHandle hDevice,
uint dwIoControlCode,
IntPtr lpInBuffer,
uint nInBufferSize,
ref VOLUME_DISK_EXTENTS lpOutBuffer,
uint nOutBufferSize,
out uint lpBytesReturned,
IntPtr lpOverlapped);


private static void GetPhysicalDriveNumberFromLogicalDriveLetter()
{
Console.WriteLine("Input logical drive letter:");
char cDrive = Console.ReadLine()[0];
if (char.IsLetter(cDrive))
{
GetDiskExtents(cDrive);
}

Console.ReadLine();
}

// Method for disk extents
private static void GetDiskExtents(char cDrive)
{
DriveInfo di = new DriveInfo(cDrive.ToString());
if (di.DriveType != DriveType.Fixed)
{
Console.WriteLine("This drive is not fixed drive.");
}

string sDrive = "\\\\.\\" + cDrive.ToString() + ":";

SafeFileHandle hDrive = CreateFileW(
sDrive,
0, // No access to drive
FILE_SHARE_READ | FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
IntPtr.Zero);

if (hDrive == null || hDrive.IsInvalid)
{
string message = GetErrorMessage(Marshal.GetLastWin32Error());
Console.WriteLine("CreateFile failed. " + message);
}

uint IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS = CTL_CODE(
IOCTL_VOLUME_BASE, 0,
METHOD_BUFFERED, FILE_ANY_ACCESS); // From winioctl.h

VOLUME_DISK_EXTENTS query_disk_extents =
new VOLUME_DISK_EXTENTS();

uint returned_query_disk_extents_size;

bool query_disk_extents_result = DeviceIoControl(
hDrive,
IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
IntPtr.Zero,
0,
ref query_disk_extents,
(uint)Marshal.SizeOf(query_disk_extents),
out returned_query_disk_extents_size,
IntPtr.Zero);

hDrive.Close();

if (query_disk_extents_result == false ||
query_disk_extents.Extents.Length != 1)
{
string message = GetErrorMessage(Marshal.GetLastWin32Error());
Console.WriteLine("DeviceIoControl failed. " + message);
}
else
{
Console.WriteLine("The physical drive number is: " +
query_disk_extents.Extents[0].DiskNumber);
}
}

Contents