GetDiskFreeSpaceEx in Windows 11 Extremely Slow with Mapped Drives Not Connected
When working with file systems, it is important to be able to retrieve information about disk space usage. One commonly used function for this purpose is the GetDiskFreeSpaceEx function in Windows. However, some users have reported that this function can be extremely slow when dealing with mapped drives that are not connected.
What is GetDiskFreeSpaceEx Function?
The GetDiskFreeSpaceEx function is a part of the Windows API and it is used to retrieve the amount of free space on a disk or a volume. It provides more precise information compared to other similar functions such as GetDiskFreeSpace or GetLogicalDriveStrings. The function takes three parameters: a pointer to the drive letter or the volume name, a pointer to a ULARGE_INTEGER structure to receive the total number of free bytes, and a pointer to a ULARGE_INTEGER structure to receive the number of free bytes after the last cluster.
The Problem with Mapped Drives
The issue arises when the mapped drives are not connected. When the function is called with a mapped drive letter, it may take a long time to return the result if the drive is not connected. This problem occurs in Windows 11 and earlier versions. It is caused by the fact that the system tries to establish a connection with the network location of the mapped drive before returning the amount of free space.
Checking if the Drive is Connected
To solve this problem, it is recommended to check if the drive is connected before calling the GetDiskFreeSpaceEx function. This can be done using the GetDriveType function. The GetDriveType function takes a drive letter as a parameter and returns the drive type such as DRIVE_REMOVABLE, DRIVE_FIXED, DRIVE_REMOTE, or DRIVE_CDROM. If the drive type is DRIVE_REMOTE, it indicates that the drive is a network drive and it may be not connected. In this case, it is possible to skip calling the GetDiskFreeSpaceEx function for this drive.
Example
BOOL bSuccess = FALSE;
ULARGE_INTEGER FreeBytesAvailable, TotalNumberOfFreeBytes, TotalNumberOfBytes;
bSuccess = GetDiskFreeSpaceEx(TEXT("X:"), &FreeBytesAvailable, &TotalNumberOfFreeBytes, &TotalNumberOfBytes);
if (bSuccess && (GetDriveType(TEXT("X:")) == DRIVE_REMOTE)) {
// Drive is a network drive, but it is connected.
} else if (!bSuccess) {
// GetDiskFreeSpaceEx failed.
} else {
// Drive is not a network drive, or it is a network drive, but it is not connected.
}
In this example, the program checks if the drive X: is a network drive and it is connected. If it is not connected, then the call to GetDiskFreeSpaceEx can be skipped.
References
- Microsoft. GetDiskFreeSpaceEx
- Microsoft. GetDriveType
- Microsoft. File System Functions