2012-01-11 67 views
3

可能重複:
How to list physical disks?清單的物理驅動器

什麼是「最好的辦法」(最快)C++的方式來列出已安裝在我的電腦上的物理驅動器?有沒有一個提升庫來做到這一點?

+1

那麼,只有提升?你允許使用哪些圖書館?什麼操作系統? – 2012-01-11 17:16:14

+0

你在使用什麼操作系統? – Cyclonecode 2012-01-11 17:17:18

+0

@TAMER先生Windows是我正在開發的系統,boost是首選方式,但其他情況下,boost不會這樣做也會被接受。 – smallB 2012-01-11 17:17:29

回答

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; 
} 

,或者使用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; 
}