列出我计算机上安装的物理驱动器

sma*_*llB 3 c++ windows boost hard-drive

可能重复:
如何列出物理磁盘?

什么是列出我计算机上安装的物理驱动器的"最佳方式"(最快)C++方式?有没有一个升级库可以做到这一点?

Sof*_*ner 11

用于GetLogicalDriveStrings()检索所有可用的逻辑驱动器.

#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;
}
Run Code Online (Sandbox Code Playgroud)

或使用 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;
}
Run Code Online (Sandbox Code Playgroud)