学习API之获取磁盘信息

#include <windows.h>
#include <stdio.h>

/************************************
* BOOL GetDiskSpaceInfo(LPSTR pszDrive)
* 功能:根据输入磁盘驱动器,获取磁盘信息
* 参数:LPSTR pszDrive
驱动器根路径,如 D:
************************************/

BOOL GetDiskSpaceInfo(LPSTR pszDrive)
{
DWORD64 qwFreeBytesToCaller, qwTotalBytes, qwFreeBytes;
DWORD dwSectPerClust, dwBtyesPerSect, dwFreeClusters, dwTotalClusters;
BOOL bResult;
bResult = GetDiskFreeSpaceEx(pszDrive,
(PULARGE_INTEGER)&qwFreeBytesToCaller,
(PULARGE_INTEGER)&qwTotalBytes,
(PULARGE_INTEGER)&qwFreeBytes);
if (bResult)
{
printf("使用GetDiskFreeSpaceEx获取磁盘信息\n");
printf("可获取的空闲空间(字节):\t%I64d\n", qwFreeBytesToCaller);
printf("空闲空间(字节):\t\t%I64d\n",qwFreeBytes);
printf("磁盘总容量(字节):\t\t%I64d\n",qwTotalBytes);
}
bResult = GetDiskFreeSpace(pszDrive,
&dwSectPerClust,
&dwBtyesPerSect,
&dwFreeClusters,
&dwTotalClusters);
if (bResult)
{
printf("\n使用GetDiskFreeSpace获取磁盘空间信息\n");
printf("空闲的镞数量:\t\t\t%d\n",dwFreeClusters);
printf("总镞数量:\t\t\t%d\n", dwTotalClusters);
printf("每镞的扇区数量:\t\t\t%d\n", dwSectPerClust);
printf("每扇区的容量(字节):\t\t\t%d\n",dwBtyesPerSect);
printf("空闲空间(字节):\t\t%I64d\n", (DWORD64)dwTotalClusters*(DWORD64)dwSectPerClust*(DWORD64)dwBtyesPerSect);
printf("磁盘总容量(字节):\t\t%I64d", (DWORD64)dwTotalClusters*(DWORD64)dwSectPerClust*(DWORD64)dwBtyesPerSect);
}
return bResult;
}
int main(int argc, PCHAR argv[])
{
GetDiskSpaceInfo(argv[1]);
}

使用实例:

posted @ 2016-05-17 20:46  mousemin  阅读(747)  评论(0编辑  收藏  举报