获取磁盘的 总容量,空余容量,已用容量 【windows】

使用windows api

输入:盘符字符串

输出:磁盘容量

 

float get_disk_spaces(const char drive_letter, float & total_space, float & used_space)
{
    BOOL fResult;
    unsigned _int64 i64FreeBytesToCaller;
    unsigned _int64 i64TotalBytes;
    unsigned _int64 i64FreeBytes;
    char dir[4] = { drive_letter, ':', '\\', '\0'};
    fResult = GetDiskFreeSpaceExA(
        dir,
        (PULARGE_INTEGER)&i64FreeBytesToCaller,
        (PULARGE_INTEGER)&i64TotalBytes,
        (PULARGE_INTEGER)&i64FreeBytes);
    if (fResult)
    {
        /*QMessageBox::about(NULL, "Information",
            ("Get disk space " + QString(dir) + QString::number(i64TotalBytes)));*/
        total_space = (float)i64TotalBytes;
        used_space = (float)(i64TotalBytes - i64FreeBytes);
        return (float)i64TotalBytes;
    }
    else
        QMessageBox::about(NULL, "Information",
            ("Failed to get disk space " + QString(dir)));
 
    return -1;
}

 

注意其中的字符数组 dir 一定要以'\0'结尾,否则程序时好时坏,因为有时dir末尾正好是0,而有时不是。

posted on 2019-01-23 16:40  liujx2019  阅读(418)  评论(0编辑  收藏  举报

导航