GetLogicalDriveStrings - 获取系统中存在的逻辑驱动器字符串
GetLogicalDriveStrings(
nBufferLength: DWORD; {缓冲区大小}
lpBuffer: PAnsiChar {缓冲区}
): DWORD;
获取磁盘名称填充到lpbuffer.
GetVolumeInformation - 读取文件系统信息
GetVolumeInformation(
lpRootPathName: PChar; {磁盘驱动器代码字符串}
lpVolumeNameBuffer: PChar; {磁盘驱动器卷标名称}
nVolumeNameSize: DWORD; {磁盘驱动器卷标名称长度}
lpVolumeSerialNumber: PDWORD; {磁盘驱动器卷标序列号}
var lpMaximumComponentLength: DWORD; {系统允许的最大文件名长度}
var lpFileSystemFlags: DWORD; {文件系统标识}
lpFileSystemNameBuffer: PChar; {文件操作系统名称}
nFileSystemNameSize: DWORD {文件操作系统名称长度}
): BOOL;
GetDiskFreeSpaceEx - 获取磁盘容量信息
BOOL GetDiskFreeSpace(
LPCTSTR lpRootPathName,
LPDWORD lpSectorsPerCluster,
LPDWORD lpBytesPerSector,
LPDWORD lpNumberOfFreeClusters,
LPDWORD lpTotalNumberOfClusters
);
demo自己看看吧,其实也没多少难点,难点就在于api的参数。继续理解中。。
Code
// disk.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
DWORD dwNeed;
DWORD dwBufferLength;
TCHAR lpBuffer[128];
LPTSTR lpDisk;
UINT uType;
ULARGE_INTEGER FreeBytesAvailable;
ULARGE_INTEGER TotalNumberOfBytes;
ULARGE_INTEGER TotalNumberOfFreeBytes;
BOOL dwReturn;
TCHAR *lpRootPathName;
TCHAR lpVolumeNameBuffer[12] = {0};
TCHAR lpFileSystemNameBuffer[10] = {0};
DWORD dwVolumeNameSize = 12;
DWORD dwFileSystemNameSize = 10;
DWORD dwVolumeSerialNumber;
DWORD dwMaximumComponentLength;
DWORD dwFileSystemFlags;
dwNeed=128;
do
{
dwBufferLength=dwNeed;
dwNeed=GetLogicalDriveStrings(dwBufferLength,lpBuffer);
if(dwNeed==0)
{
return FALSE;
}
}while(dwNeed>dwBufferLength);
for(lpDisk=lpBuffer;*lpDisk;lpDisk+=wcslen(lpDisk)+1)
{
uType=GetDriveType(lpDisk);
switch(uType)
{
case DRIVE_UNKNOWN:
wcout<<*lpDisk<<"Type:UNKNOWN";
break;
case DRIVE_NO_ROOT_DIR:
wcout<<*lpDisk<<":Type:NO_ROOT_DIR";
break;
case DRIVE_REMOVABLE:
wcout<<*lpDisk<<":Type:REMOVABLE";
break;
case DRIVE_FIXED:
wcout<<*lpDisk<<":Type:FIXED";
break;
case DRIVE_REMOTE:
wcout<<*lpDisk<<":Type:REMOTE";
break;
case DRIVE_CDROM:
wcout<<*lpDisk<<":Type:CDROM";
break;
case DRIVE_RAMDISK:
wcout<<*lpDisk<<":Type:RAMDISK";
break;
default:
cout<<"Type:ERROR";
break;
}
lpRootPathName = lpDisk;
dwReturn = GetVolumeInformation(lpRootPathName,lpVolumeNameBuffer,
dwVolumeNameSize,&dwVolumeSerialNumber,
&dwMaximumComponentLength,&dwFileSystemFlags,
lpFileSystemNameBuffer,dwFileSystemNameSize);
wcout<<"---"<<dwVolumeSerialNumber<<"---"<<lpFileSystemNameBuffer;
dwReturn = GetDiskFreeSpaceEx(lpRootPathName,&FreeBytesAvailable,
&TotalNumberOfBytes,&TotalNumberOfFreeBytes);
wcout<<"---"<<TotalNumberOfBytes.QuadPart/(1024*1024)<<"---"<<TotalNumberOfFreeBytes.QuadPart/(1024*1024)<<"---"<<100-100*TotalNumberOfFreeBytes.QuadPart/TotalNumberOfBytes.QuadPart<<endl;
}
return 0;
}
demo中有点小错误,还没改,会改的提出下