如何用C#检查硬盘是否是固态硬盘SSD

您所在的位置:网站首页 disengaged的ant 如何用C#检查硬盘是否是固态硬盘SSD

如何用C#检查硬盘是否是固态硬盘SSD

2023-08-13 13:03| 来源: 网络整理| 查看: 265

如何用C#检查硬盘是否是固态硬盘SSD

By Dawei XU

Published Jul 9 2015

Contents

在我的上一篇文章用C#来查看电脑硬件和系统信息中介绍了Environment和ManagementClass两个类。那我们能不能通过这两个类得到硬盘是不是固态硬盘SSD呢?答案是否定的。

那我们怎么才能判断一个硬盘是不是固态硬盘SSD呢?我们知道Windows的磁盘碎片整理对SSD是默认关闭的,那么Windows是怎么判断硬盘是不是固态硬盘呢?Windows 7 Disk Defragmenter User Interface Overview介绍了磁盘碎片整理的算法,它通过下面两种方式来判断硬盘是不是固态硬盘SSD。

Volumes on disks whose driver reports “no seek penalty”.就是说如果硬盘没有查找开销,那么就认为是SSD。具体定义如下:

Disk port driver needs to send a valid response to IOCTL_STORAGE_QUERY_PROPERTY call. Disk Defragmenter issues IOCTL_STORAGE_QUERY_PROPERTY request with QueryType = PropertyStandardQuery and PropertyId = StorageDeviceSeekPenaltyProperty.

A valid response involves populating a DEVICE_SEEK_PENALTY_DESCRIPTOR structure with accurate version and size data. If the IncursSeekPenalty member is set to false, the disk is considered a SSD.

Volumes on ATA/SATA disks that report a nominal media rotation rate of 1.就是说如果硬盘的转速是1,就认为是固态硬盘SSD。具体定义如下:

If the port driver does not return valid data for StorageDeviceSeekPenaltyProperty, Disk Defragmenter looks at the result of directly querying the device through the ATA IDENTIFY DEVICE command. Defragmenter issues IOCTL_ATA_PASS_THROUGH request and checks IDENTIFY_DEVICE_DATA structure. If the NomimalMediaRotationRate is set to 1, this disk is considered a SSD.

The latest SSDs will respond to the command by setting word 217 (which is used for reporting the nominal media rotation rate to 1). The word 217 was introduced in 2007 in the ATA8-ACS specification.

我们也可以用类似的方法来判断硬盘是不是SSD,下面的代码修改自Tell whether SSD or not in C#。主要函数是isSSDBySeekPenalty和isSSDByNominalMediaRotationRate。

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276// For CreateFile to get handle to driveprivate 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 codesprivate 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 static uint CTL_CODE(uint DeviceType, uint Function, uint Method, uint Access){ return ((DeviceType


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3