获取磁盘容量

c++

Posted by YiMiTuMi on October 9, 2021

获取磁盘容量

保存磁盘容量结构体:

//磁盘信息
typedef struct _DISK_DATA_INFO
{
	wstring strDiskPath;          //磁盘路径
	DWORD64 dwDiskTotalCapacity; //磁盘总容量 (字节)
	DWORD64 dwDiskUseCapacity;//磁盘使用容量   (字节)
	DWORD64 dwDiskResidueCapacity;//磁盘可用容量  (字节)

}DISK_DATA_INFO, *PDISK_DATA_INFO;

获取磁盘容量函数:

//获取磁盘信息
void GetDrivesInfo(vector<DISK_DATA_INFO> &vecDrivesInfo)
{
	TCHAR LogicalDrives[MAX_PATH] = { 0 };
	int nLen = GetLogicalDriveStringsW(MAX_PATH-1, LogicalDrives);
	int i = 0;

	do 
	{
		TCHAR wcharDrives[MAX_PATH];
		wstring wstrDrive;
		wmemcpy(wcharDrives, LogicalDrives + i, 4);

		wstrDrive = wcharDrives;

		//判断当前磁盘类型
		int nType = GetDriveTypeW(strDrive.GetString());

		if (nType == DRIVE_FIXED) //硬盘
		{
			DISK_DATA_INFO struct_DiskDataInfo;
			BOOL bResult = GetDiskFreeSpaceExW(wstrDrive.c_str(), 
				(PULARGE_INTEGER)&struct_DiskDataInfo.dwDiskResidueCapacity, 
				(PULARGE_INTEGER)&struct_DiskDataInfo.dwDiskTotalCapacity, 
				(PULARGE_INTEGER)&struct_DiskDataInfo.dwDiskUseCapacity) ;

			if (bResult)
			{
				struct_DiskDataInfo.strDiskPath = wstrDrive;
				vecDrivesInfo.push_back(struct_DiskDataInfo);
			}
		}

		i += 4;

	} while (i<nLen);
}

蓝色妖姬 – 相守