列出所有物理驱动器(Windows) [英] Listing All Physical Drives (Windows)
问题描述
我怎样才能得到所有的物理驱动器路径(\\\\。\\ PhysicalDriveX)在Windows计算机上,用C / C ++?
How can I get all the physical drive paths (\\.\PhysicalDriveX) on a Windows computer, with C/C++?
在这个问题建议让逻辑驱动器盘符,然后让对应于安装驱动器的物理驱动器。问题是,我想要得到的所有的
物理驱动器连接到电脑上,包括未安装的驱动器。
The answers in this question suggest getting the logical drive letter, and then getting the physical drive corresponding to that mounted drive. The problem is, I want to get all physical drives connected to the computer, including drives that are not mounted.
其他答案建议从0-15递增值,如果一个驱动器存在有检查(\\\\。\\ PhysicalDrive0,\\\\。\\ PhysicalDrive1,...)或调用WMIC列出所有驱动器。[
Other answers suggest incrementing a value from 0-15 and checking if a drive exists there (\\.\PhysicalDrive0, \\.\PhysicalDrive1, ...) or calling WMIC to list all the drives.[
由于这些看起来他们会的工作,他们似乎像他们根本没把最好的办法。有没有 GetPhysicalDrives
一个简单的函数,这样只是返回的std ::字符串
的包含路径的矢量所有的物理驱动器?
As these seem like they would work, they seem like they are not the best approach to be taking. Is there not a simple function such as GetPhysicalDrives
that simply returns a vector of std::string
's containing the paths of all the physical drives?
推荐答案
您可以使用 QueryDosDevice
。根据描述,你期望这个名单之类的东西 C:
和 D:
,但它也将列出的东西像 PhysicalDrive0
, PhysicalDrive1
等。
You can use QueryDosDevice
. Based on the description, you'd expect this to list things like C:
and D:
, but it will also lists things like PhysicalDrive0
, PhysicalDrive1
and so on.
主要的缺点是,它也列出了很多你可能不关心其他设备的名称,所以(比如)我的机器上,我得到的近600设备名称列表,其中只有一个相当很小的比例是关系到你关心的东西。
The major shortcoming is that it will also list a lot of other device names you probably don't care about, so (for example) on my machine, I get a list of almost 600 device names, of which only a fairly small percentage is related to what you care about.
万一你关心,一些(旧)样本code:
Just in case you care, some (old) sample code:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
int main(int argc, char **argv) {
char physical[65536];
char logical[65536];
if ( argc > 1) {
for (int i=1; i<argc; i++) {
QueryDosDevice(argv[i],logical, sizeof(logical));
std::cout << argv[i] << " : \t" << logical << std::endl << std::endl;
}
return 0;
}
QueryDosDevice(NULL, physical, sizeof(physical));
std::cout << "devices: " << std::endl;
for (char *pos = physical; *pos; pos+=strlen(pos)+1) {
QueryDosDevice(pos, logical, sizeof(logical));
std::cout << pos << " : \t" << logical << std::endl << std::endl;
}
return 0;
}
但是,如果我运行这个像`devlist | grep的^物理,它列出了物理驱动器。
However, if I run this like `devlist | grep "^Physical", it lists the physical drives.
这篇关于列出所有物理驱动器(Windows)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!