Get a list of available drives and their sizes

I know that you can use a combination of GetLogicalDrives () and GetDiskFreeSpaceEx () to get a list of disks and their sizes. I use GetDiskFreeSpaceEx () without any problems, but when I try to use GetLogicalDrives (), I have a problem: I do not want to check every possible letter to see if it exists or not before passing it to GetDiskFreeSpaceEx ().

Is there an easier way to get a list of disks (disks) in the system and what are their sizes? I am using C on Windows.

I want to make something clear, I'm KNOW , it might be easier using C # and WMI, I'm not interested in this, so please do not post this as a possible solution. If you want to specify how this is done in C and WMI, go for it. NO C ++ or C # thanks! (as someone did in my previous question )

+3
source share
3 answers

GetLogicalDrives () is the system API for this. A simple for () loop converts its result into drive letters, for example:

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, .....);
    }
}

And if you are not satisfied with the level of service provided by Stack Overflow, feel free to ask for a refund.

+5
source

GetLogicalDriveStrings - , .

UPDATE:

, , 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
+7

, . free (), ​​ 'singleDriveString':

#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;

} 
+1

Source: https://habr.com/ru/post/1733262/


All Articles