问题
Possible Duplicate:
How to list physical disks?
What is the "best way" (fastest) C++ way to List physical drives installed on my computer? Is there a boost library to do that?
回答1:
use GetLogicalDriveStrings()
to retrieve all the available logical drives.
#include <windows.h>
#include <stdio.h>
DWORD mydrives = 100;// buffer length
char lpBuffer[100];// buffer for drive string storage
int main()
{
DWORD test = GetLogicalDriveStrings( mydrives, lpBuffer);
printf("The logical drives of this machine are:\n\n");
for(int i = 0; i<100; i++) printf("%c", lpBuffer[i]);
printf("\n");
return 0;
}
or use GetLogicalDrives()
#include <windows.h>
#include <direct.h>
#include <stdio.h>
#include <tchar.h>
// initial value
TCHAR szDrive[ ] = _T(" A:");
int main()
{
DWORD uDriveMask = GetLogicalDrives();
printf("The bitmask of the logical drives in hex: %0X\n", uDriveMask);
printf("The bitmask of the logical drives in decimal: %d\n", uDriveMask);
if(uDriveMask == 0)
printf("\nGetLogicalDrives() failed with failure code: %d\n", GetLastError());
else
{
printf("\nThis machine has the following logical drives:\n");
while(uDriveMask)
{// use the bitwise AND, 1–available, 0-not available
if(uDriveMask & 1)
printf("%s\n",szDrive);
// increment...
++szDrive[1];
// shift the bitmask binary right
uDriveMask >>= 1;
}
printf("\n ");
}
return 0;
}
回答2:
One possibility is to use WMI to enumerate the instances of Win32_DiskDrive.
来源:https://stackoverflow.com/questions/8823597/listing-physical-drives-installed-on-my-computer