如何在 C++ 中使用 WMI 获得硬盘所有逻辑驱动器的空闲 space?
How to get free space of all logical drives of hard disk using WMI in C++?
我想使用 C++ 和 WMI 计算整个硬盘的空闲 space。
例如。
如果 HDD 包含 3 个逻辑驱动器,请说 C:、D:、E:
每个逻辑驱动器都有以下配置。
总计 Space 免费 Space
C: 10GB 5GB
D: 20GB 8GB
E: 15GB 7GB
所以我需要获取空闲硬盘驱动器 space,即所有驱动器 C、D 和 E 的空闲 space。
所以它应该 return 5+8+7 = 20 GB。
我也不知道该硬盘驱动器存在哪些逻辑驱动器。
非 WMI ay 更容易。
您可以使用GetLogicalDriveStrings
(https://msdn.microsoft.com/en-us/library/windows/desktop/aa364975(v=vs.85).aspx)获取系统中的所有驱动器。
接下来使用 GetDiskFreeSpace
(https://msdn.microsoft.com/en-us/library/windows/desktop/aa364935(v=vs.85).aspx) 查找特定驱动器的空闲 space。
如果你真的想(必须)坚持使用 WMI,页面 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 有一个 FreeSpace
成员。
这是一个完全可用的功能,可以让您在所有驱动器中免费 space。
它以 CString 的形式生成结果,但当然你可以 return 一个数字(字节数)。
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 '[=10=]' 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;
}
我想使用 C++ 和 WMI 计算整个硬盘的空闲 space。
例如。 如果 HDD 包含 3 个逻辑驱动器,请说 C:、D:、E: 每个逻辑驱动器都有以下配置。
总计 Space 免费 Space
C: 10GB 5GB D: 20GB 8GB E: 15GB 7GB
所以我需要获取空闲硬盘驱动器 space,即所有驱动器 C、D 和 E 的空闲 space。
所以它应该 return 5+8+7 = 20 GB。
我也不知道该硬盘驱动器存在哪些逻辑驱动器。
非 WMI ay 更容易。
您可以使用GetLogicalDriveStrings
(https://msdn.microsoft.com/en-us/library/windows/desktop/aa364975(v=vs.85).aspx)获取系统中的所有驱动器。
接下来使用 GetDiskFreeSpace
(https://msdn.microsoft.com/en-us/library/windows/desktop/aa364935(v=vs.85).aspx) 查找特定驱动器的空闲 space。
如果你真的想(必须)坚持使用 WMI,页面 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 有一个 FreeSpace
成员。
这是一个完全可用的功能,可以让您在所有驱动器中免费 space。 它以 CString 的形式生成结果,但当然你可以 return 一个数字(字节数)。
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 '[=10=]' 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;
}