作者:安靜
VC6.0 W2K編譯通過
這類小程序,能用的地方很多.所以就寫一個完整的代碼 給大家參考參考
#include "stdafx.h" #include <iostream.h> const DWORD MAXLEN = 100;
void ShowDriveInfo(LPTSTR drive) { //輸出設備類型 UINT result; result = GetDriveType(drive);
if (result == DRIVE_REMOVABLE) cout << "可移動設備" ; else if (result == DRIVE_FIXED) cout << "硬盤"; else if (result == DRIVE_REMOTE) cout << "網絡驅動器" ; else if (result == DRIVE_CDROM ) cout << " 光驅"; else if (result == DRIVE_RAMDISK) cout << "Ram Disk"; else if (result == DRIVE_UNKNOWN) cout << "未知的設備"; else return; cout << '\t';
//給出空間信息
unsigned __int64 i64FreeBytesToCaller; unsigned __int64 i64TotalBytes; unsigned __int64 i64FreeBytes;
DWORD dwSectPerClust; DWORD dwBytesPerSect; DWORD dwFreeClusters; DWORD dwTotalClusters;
DWORD tempTotal; DWORD tempFree;
BOOL fResult; typedef DWORD (WINAPI * GETDISKFREESPACEEX)(LPCTSTR , PULARGE_INTEGER , PULARGE_INTEGER , PULARGE_INTEGER );
GETDISKFREESPACEEX pGetDiskFreeSpaceEx;
pGetDiskFreeSpaceEx = (GETDISKFREESPACEEX)GetProcAddress( GetModuleHandle("kernel32.dll"), "GetDiskFreeSpaceExA");
if (pGetDiskFreeSpaceEx) //如果是Windows NT and Windows 2000使用 GetDiskFreeSpaceEx { fResult = pGetDiskFreeSpaceEx ( (LPCTSTR)drive, (PULARGE_INTEGER)&i64FreeBytesToCaller, (PULARGE_INTEGER)&i64TotalBytes, (PULARGE_INTEGER)&i64FreeBytes);
tempTotal = i64TotalBytes/1024 ; tempFree = i64FreeBytes/1024;
}
else //如果是Windows 95 OSR2 and Windows 98 使用 GetDiskFreeSpace { fResult = GetDiskFreeSpace (drive, &dwSectPerClust, &dwBytesPerSect, &dwFreeClusters, &dwTotalClusters);
tempTotal = dwTotalClusters*dwBytesPerSect*dwSectPerClust/1024; tempFree = dwFreeClusters*dwSectPerClust*dwBytesPerSect/1024;
} if(fResult) { cout<<"全部磁盤容量是 "<<((float)(tempTotal)/1024/1024)<<"GB\t"; cout<<"空余磁盤容量是 "<<((float)(tempFree)/1024/1024)<<"GB\t"; } cout<<endl;
//像光驅,軟盤,不放盤的時候,GetDiskFreeSpace(Ex)會出錯.
} void GetAllDrive() {
int len = -1; char drive[4] ="AAA" ; LPTSTR lpDriveString = new char[MAXLEN]; DWORD dwBufferLen = MAXLEN ; len = GetLogicalDriveStrings(dwBufferLen,lpDriveString); //取得全部的盤符 if (len < 0) cout << "操作失敗" <<endl; if (len > MAXLEN) cout << "Buffer不足" <<endl; if (len > 0) { for (int i = 0;i < len;i = i+4) //得到的盤符有4個字符組成 A:\(null) { drive[0] = lpDriveString[i]; //A drive[1] = lpDriveString[i+1];//: drive[2] = lpDriveString[i+2];// 第三個\ //drive[3] = '\0'; // 第四個為null字符 cout<<drive<<'\t';
ShowDriveInfo(drive); } } ///////////////////////////////////////////// //另外一種方法. /////////////////////////////////////////////// /* char D = 'A'; char Drive[4]="X:\\"; for(int i = 0; i < 26;i++) { Drive[0] = (char)(D+i); cout<<Drive<<'\t'; ShowDriveInfo(Drive); } */
}
void main() { GetAllDrive(); }
|