获取可用驱动器及其大小的列表

时间:2010-02-17 20:23:55

标签: c windows winapi

我知道您可以使用GetLogicalDrives()和GetDiskFreeSpaceEx()的组合来获取驱动器及其大小的列表。我一直在使用GetDiskFreeSpaceEx()没有问题但是当我尝试使用GetLogicalDrives()时遇到了一个问题:我不想在将它传递给GetDiskFreeSpaceEx之前检查每个可能的字母以查看它是否存在()。

是否有更简单的方法来获取系统上的驱动器(磁盘)列表以及它们的大小?我在Windows上使用C语言。

我想说清楚一点,我知道使用C#和WMI可能更容易,我对此没有兴趣,所以请不要将其作为可能的解决方案发布。如果你想指出如何在C和WMI中完成,那就去吧。没有C ++或C#谢谢! (就像某人在my previous question中所做的那样)

3 个答案:

答案 0 :(得分:8)

您可以使用GetLogicalDriveStrings - 这将返回一个包含系统上所有有效驱动器号的缓冲区。

更新:

以下是我编写的示例程序,它使用GetLogicalDriveStrings枚举驱动器并输出有关它们的一些基本信息。

#include <windows.h>
#include <malloc.h>
#include <stdio.h>

int __cdecl main()
{
    DWORD cchBuffer;
    WCHAR* driveStrings;
    UINT driveType;
    PWSTR driveTypeString;
    ULARGE_INTEGER freeSpace;

    // Find out how big a buffer we need
    cchBuffer = GetLogicalDriveStrings(0, NULL);

    driveStrings = (WCHAR*)malloc((cchBuffer + 1) * sizeof(TCHAR));
    if (driveStrings == NULL)
    {
        return -1;
    }

    // Fetch all drive strings    
    GetLogicalDriveStrings(cchBuffer, driveStrings);

    // Loop until we find the final '\0'
    // driveStrings is a double null terminated list of null terminated strings)
    while (*driveStrings)
    {
        // Dump drive information
        driveType = GetDriveType(driveStrings);
        GetDiskFreeSpaceEx(driveStrings, &freeSpace, NULL, NULL);

        switch (driveType)
        {
        case DRIVE_FIXED:
            driveTypeString = L"Hard disk";
            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;
        }

        printf("%S - %S - %I64u GB free\n", driveStrings, driveTypeString,
                  freeSpace.QuadPart / 1024 / 1024 / 1024);

        // Move to next drive string
        // +1 is to move past the null at the end of the string.
        driveStrings += lstrlen(driveStrings) + 1;
    }

    free(driveStrings);

    return 0;

}

在我的机器上输出:

C:\ - Hard disk - 181 GB free
D:\ - CD/DVD - 0 GB free
E:\ - Hard disk - 806 GB free

答案 1 :(得分:5)

GetLogicalDrives()是系统提供的API。一个简单的for()循环会将其结果转换为驱动器号,如下所示:

DWORD d = GetLogicalDrives();
int i;
TCHAR Drive[] = _T("A:\\");
for(i=0;i<26;i++)
{
    if(d & (1<<i))
    {
        Drive[0] = _T('A')+i;
        GetDiskFreeSpaceEx(Drive, .....);
    }
}

如果您对Stack Overflow提供的服务水平不满意,请随时要求退款。

答案 2 :(得分:2)

这是Michael的代码rejected作为编辑。它会在free ()上出现故障,除非虚拟变量“singleDriveString”#39;插入如此:

#include <windows.h>
#include <malloc.h>
#include <stdio.h>

int __cdecl main()
{
DWORD cchBuffer;
WCHAR* driveStrings;
UINT driveType;
PWSTR driveTypeString;
ULARGE_INTEGER freeSpace;

// Find out how big a buffer we need
cchBuffer = GetLogicalDriveStrings(0, NULL);

driveStrings = (WCHAR*)malloc((cchBuffer + 1) * sizeof(TCHAR));
if (driveStrings == NULL)
{
    return -1;
}

// Fetch all drive strings    
GetLogicalDriveStrings(cchBuffer, driveStrings);

// Loop until we find the final '\0'
// driveStrings is a double null terminated list of null terminated strings)
wchar_t * singleDriveString = driveStrings;
while (*singleDriveString)
{
    // Dump drive information
    driveType = GetDriveType(singleDriveString);
    GetDiskFreeSpaceEx(singleDriveString, &freeSpace, NULL, NULL);

    switch (driveType)
    {
    case DRIVE_FIXED:
        driveTypeString = L"Hard disk";
        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;
    }

    printf("%S - %S - %I64u GB free\n", singleDriveString, driveTypeString,
              freeSpace.QuadPart / 1024 / 1024 / 1024);

    // Move to next drive string
    // +1 is to move past the null at the end of the string.
    singleDriveString += lstrlen(singleDriveString) + 1;
}

free(driveStrings);

return 0;

}