在QT中获取磁盘类型 [英] Get disk type in QT
问题描述
我想在QT中获取磁盘类型(SSD或HDD).我已经检查了 QStorageInfo
,但是找不到适合我用途的任何东西.顺便说一下,我需要在Windows和Linux上都可以使用的解决方案.
I want to get disk type (SSD or HDD) in QT. I've checked QStorageInfo
but I could not find anything useful for my purpose. By the way I need a solution that's work on both Windows and Linux.
推荐答案
对于linux,您可以通过读取特殊文件来判断内核是否已检测到SSD磁盘
For linux, you can tell whether the kernel has detected a SSD disk by reading special file
/sys/block/<disk>/queue/rotational
例如,cat/sys/block/sda/queue/rotational
如果sda磁盘是HDD则写入1
,如果是SSD则写入0
For instance, cat /sys/block/sda/queue/rotational
writes 1
if sda disk is HDD and 0
if it's SSD.
对于Windows,您可以通过打开特殊文件 \\.\ PhysicalDrive< number>
(例如 \\.\ PhysicalDrive0
)来打开驱动器.可以与 <使用 <代码> StorageDeviceSeekPenaltyProperty 可能就是您想要的,因为HDD会寻道,而SSD则不会.las,我现在没有Windows环境可以测试.
For Windows, you can open a drive by opening special file \\.\PhysicalDrive<number>
, for instance \\.\PhysicalDrive0
. It can the be used with DeviceIOControl
to query properties, using IOCTL_STORAGE_QUERY_PROPERTY
IO control. It seems StorageDeviceSeekPenaltyProperty
may be what you're after, as HDD have a seek penalty while SSD don't. Alas I don't have a windows environment around to test right now.
对于可移植性,我高度怀疑这种与系统有关的信息是否可以通过便携式方式获得.您必须使用 #ifdef
/ #else
/ #endif
宏来根据当前目标选择实现.
For portability, I highly doubt such system-dependent information is available in a portable way. You'll have to use #ifdef
/#else
/#endif
macros to select an implementation depending on current target.
这篇关于在QT中获取磁盘类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!