void test2()
{
DWORD dwLen = GetLogicalDriveStrings(0, NULL); //获取系统字符串长度.
char * pszDriver = new char[dwLen]; //构建一个相应长度的数组.
GetLogicalDriveStrings(dwLen, pszDriver); //获取盘符字符串.
vector<string> v;
while (*pszDriver != '\0')
{
cout << pszDriver << " ---- " << endl;
v.push_back(pszDriver);
pszDriver += strlen(pszDriver) + 1; //定位到下一个字符串.加一是为了跳过'\0'字符串.
}
int DType;
int si = 0;
BOOL fResult;
unsigned _int64 i64FreeBytesToCaller;
unsigned _int64 i64TotalBytes;
unsigned _int64 i64FreeBytes;
for (int i = 0; i < dwLen / 4; ++i)
//为了显示每个驱动器的状态,则通过循环输出实现,由于DStr内部保存的数据是A:\NULLB:\NULLC:\NULL,这样的信息,所以DSLength/4可以获得具体大循环范围
{
DType = GetDriveType(v[i].c_str());
//GetDriveType函数,可以获取驱动器类型,参数为驱动器的根目录
if (DType == DRIVE_FIXED)
{
cout << "硬盘";
}
else if (DType == DRIVE_CDROM)
{
cout << "光驱";
}
else if (DType == DRIVE_REMOVABLE)
{
cout << "可移动式磁盘";
}
else if (DType == DRIVE_REMOTE)
{
cout << "网络磁盘";
}
else if (DType == DRIVE_RAMDISK)
{
cout << "虚拟RAM磁盘";
}
else if (DType == DRIVE_UNKNOWN)
{
cout << "未知设备";
}
fResult = GetDiskFreeSpaceEx(
v[i].c_str(),
(PULARGE_INTEGER)&i64FreeBytesToCaller,
(PULARGE_INTEGER)&i64TotalBytes,
(PULARGE_INTEGER)&i64FreeBytes);
//GetDiskFreeSpaceEx函数,可以获取驱动器磁盘的空间状态,函数返回的是个BOOL类型数据
if (fResult)//通过返回的BOOL数据判断驱动器是否在工作状态
{
cout << " totalspace:" << (float)i64TotalBytes / 1024 / 1024 << " MB";//磁盘总容量
cout << " freespace:" << (float)i64FreeBytesToCaller / 1024 / 1024 << " MB";//磁盘剩余空间
}
else
{
cout << " 设备未准备好";
}
cout << endl;
si += 4;
}
}
C++ 获取磁盘盘符以及剩余空间
最新推荐文章于 2024-02-25 20:43:32 发布