[WINAPI]取得磁碟空間

可使用GetDiskFreeSpaceEx()來取得目前可使用的磁碟空間、總磁碟空間以及總剩餘磁碟空間。

使用方式如下:


    char szDiskDir[MAX_PATH]  = "C:\\";

    //A pointer to a variable that receives the total number of free bytes on a disk that are available to the user who is associated with the calling thread.
    //This parameter can be NULL.
    //If per-user quotas are being used, this value may be less than the total number of free bytes on a disk.
    __int64 n64AvailableFreeDiskBytes;       

    //A pointer to a variable that receives the total number of bytes on a disk that are available to the user who is associated with the calling thread.
    //This parameter can be NULL.
    //If per-user quotas are being used, this value may be less than the total number of bytes on a disk.
    //To determine the total number of bytes on a disk or volume, use IOCTL_DISK_GET_LENGTH_INFO.        
    __int64 n64TotalDiskBytes;

    //A pointer to a variable that receives the total number of free bytes on a disk.
    //This parameter can be NULL.
    __int64 n64TotalFreeDiskBytes;

    if ( !::GetDiskFreeSpaceEx(szDiskDir, (PULARGE_INTEGER) &n64AvailableFreeDiskBytes, (PULARGE_INTEGER) &n64TotalDiskBytes,
                                                            (PULARGE_INTEGER) &n64TotalFreeDiskBytes) )
    {
        printf("Fail to get disk fress space in %s (Err = %d).\n", szDiskDir, ::GetLastError());
    }else{
        printf("%s has %lld bytes available free disk.\nTotal disk size is %lld bytes.\nTotal free space is %lld bytes.\n", 
                    szDiskDir, n64AvailableFreeDiskBytes, n64TotalDiskBytes, n64TotalFreeDiskBytes);
    }

No comments:

Post a Comment

Build docker image from multiple build contexts

Build docker image from multiple build contexts ...