Windows 枚举系统磁盘,计算剩余空间

枚举系统所有驱动器名字,并计算各个驱动器的总大小和剩余大小

代码如下:

 

#include <vector>
#include <string>
#include <Windows.h>
#include <tchar.h>
#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	TCHAR  drives[128];//存储所以驱动器名称
	wchar_t* pDrive;//驱动器指针

	//取得系统的第一个逻辑驱动器
	if (!GetLogicalDriveStrings(sizeof(drives), drives))
	{
		std::cout << "获取驱动器失败" << std::endl;
		return false;
	}

	std::vector<std::wstring> diskName;
	pDrive = drives; //指向第一个逻辑驱动器
	while(*pDrive)
	{
		//将驱动器名称加入列表中
		diskName.push_back(pDrive);

		//指向下一个驱动器标识符
		pDrive += wcslen(pDrive) + 1;
	}

	unsigned __int64 nFreeBytesAvailableToCaller;
	unsigned __int64 nTotalNumberOfBytes;
	unsigned __int64 nTotalNumberOfFreeBytes;

	for (size_t i=0; i<diskName.size(); ++i)
	{
		std::wcout << diskName[i].c_str() << std::endl;

		int ntype = GetDriveType(diskName[i].c_str());
		if(ntype == DRIVE_FIXED)
		{
			std::cout << "硬盘" << std::endl;
		}
		else if(ntype == DRIVE_CDROM)
		{
			std::cout << "光驱" << std::endl;
		}
		else if(ntype == DRIVE_REMOVABLE)
		{
			std::cout << "可移动式磁盘" << std::endl;
		}
		else if(ntype == DRIVE_REMOTE)
		{
			std::cout << "网络磁盘" << std::endl;
		}
		else if(ntype == DRIVE_RAMDISK)
		{
			std::cout << "虚拟RAM磁盘" << std::endl;
		}
		else if (ntype == DRIVE_UNKNOWN)
		{
			std::cout << "未知设备" << std::endl;
		}

		//获取驱动器磁盘的空间状态
		BOOL bResult = GetDiskFreeSpaceEx(
			diskName[i].c_str(),
			(PULARGE_INTEGER)&nFreeBytesAvailableToCaller,
			(PULARGE_INTEGER)&nTotalNumberOfBytes,
			(PULARGE_INTEGER)&nTotalNumberOfFreeBytes);

		//判断驱动器是否在工作状态
		if (bResult)
		{
			//磁盘总容量
			std::cout << "磁盘总容量: " << (float)nTotalNumberOfBytes / 1024 / 1024 / 1024 << " GB" << std::endl;
			//磁盘剩余空间
			std::cout << "磁盘剩余空间: " << (float)nTotalNumberOfFreeBytes / 1024 / 1024 / 1024 << " GB" << std::endl;
		}
		else
		{
			std::cout << "设备未准备好" << std::endl;
		}
	}

	system("pause");
	return 0;
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hellokandy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值