[英]How to get free space of all logical drives of hard disk using WMI in C++?
I would like to calculate free space of entire hard drive using c++ and WMI.我想使用 c++ 和 WMI 计算整个硬盘的可用空间。
For example.例如。 if HDD contains 3 logical drives say C: , D: , E: and each logical drive has below configuration.
如果 HDD 包含 3 个逻辑驱动器,比如 C: , D: , E: 并且每个逻辑驱动器都有以下配置。
drive Total Space Free Space驱动器总空间可用空间
C: 10GB 5 GB D: 20GB 8 GB E: 15GB 7 GB C:10GB 5GB D:20GB 8GB E:15GB 7GB
So I need to fetch the free hard drive space ie free space of all drives C,D and E.所以我需要获取可用硬盘空间,即所有驱动器 C、D 和 E 的可用空间。
SO it should return 5+8+7 = 20 GB.所以它应该返回 5+8+7 = 20 GB。
Also I don't know what all logical drives exists for that Hard drive.我也不知道该硬盘驱动器存在哪些逻辑驱动器。
The non WMI ay is much easier. 非WMI容易得多。
You can use GetLogicalDriveStrings
( https://msdn.microsoft.com/en-us/library/windows/desktop/aa364975(v=vs.85).aspx ) to get all the drives in the system. 您可以使用
GetLogicalDriveStrings
( https://msdn.microsoft.com/zh-cn/library/windows/desktop/aa364975 ( v= GetLogicalDriveStrings
) GetLogicalDriveStrings
)来获取系统中的所有驱动器。
Next use GetDiskFreeSpace
( https://msdn.microsoft.com/en-us/library/windows/desktop/aa364935(v=vs.85).aspx ) to find the free space of a specific drive. 接下来使用
GetDiskFreeSpace
( https://msdn.microsoft.com/zh-cn/library/windows/desktop/aa364935 ( v= GetDiskFreeSpace
) GetDiskFreeSpace
)查找特定驱动器的可用空间。
If yo really want to (have to) stick to WMI, the page https://msdn.microsoft.com/en-us/library/windows/desktop/aa393244(v=vs.85).aspx will give some directions on how to instantiate WMI classes, and you will need to work with the Win32_LogicalDisk
( https://msdn.microsoft.com/en-us/library/windows/desktop/aa394173(v=vs.85).aspx ) class which has a FreeSpace
member. 如果您真的想(必须)坚持使用WMI,则页面https://msdn.microsoft.com/zh-cn/library/windows/desktop/aa393244(v=vs.85).aspx将提供有关以下方面的说明如何实例化WMI类,您将需要使用
Win32_LogicalDisk
( https://msdn.microsoft.com/zh-cn/library/windows/desktop/aa394173 ( v= Win32_LogicalDisk
) Win32_LogicalDisk
)类, FreeSpace
成员。
Here is a fully working function which will get you the free space in all drives.这是一个完全可用的 function,它将为您提供所有驱动器中的可用空间。 It generates the result as a CString but of course you can return a number (of bytes).
它以 CString 的形式生成结果,但您当然可以返回一个数字(字节数)。
CString GetFreeDiskSpace()
{
CString str_result{ L"" };
DWORD cchBuffer;
WCHAR stddriveStrings[2048];
WCHAR *driveSetings = &stddriveStrings[0];
UINT driveType;
PWSTR driveTypeString;
ULARGE_INTEGER freeSpace;
// Find out the required buffer size that we need
cchBuffer = GetLogicalDriveStrings(0, NULL);
// Fetch all drive letters as strings
GetLogicalDriveStrings(cchBuffer, driveSetings);
// Loop until we reach the end (by the '\0' char)
// driveStrings is a double null terminated list of null terminated strings)
while (*driveSetings)
{
// Dump drive information
driveType = GetDriveType(driveSetings);
GetDiskFreeSpaceEx(driveSetings, &freeSpace, NULL, NULL);
switch (driveType)
{
case DRIVE_FIXED:
driveTypeString = L"Hard Drive";
break;
case DRIVE_CDROM:
driveTypeString = L"CD/DVD";
break;
case DRIVE_REMOVABLE:
driveTypeString = L"Removable";
break;
case DRIVE_REMOTE:
driveTypeString = L"Network";
break;
default:
driveTypeString = L"Unknown";
break;
}
str_result.Format(L"%s\n%s - %s - %I64u GB free",
str_result, driveSetings, driveTypeString,
freeSpace.QuadPart / 1024 / 1024 / 1024);
// Move to next drive string
// +1 is to move past the null at the end of the string.
driveSetings += _tcsclen(driveSetings) + 1;
}
return str_result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.