【Windows系统】C++ Qt方式获取所有磁盘使用详情

磁盘的使用详情,及磁盘的总容量、剩余容量和剩余可用容量。代码如下#include windows.h#include QVectorstruct DriveInfo{QString sDriName;quint64 uiTotal;quint64 uiFree;quint64 uiFree2Caller;DriveInf...

磁盘的使用详情,及磁盘的总容量、剩余容量和剩余可用容量。

代码如下

#include <windows.h>
#include <QVector>

struct DriveInfo
{
    QString sDriName;
    quint64 uiTotal;
    quint64 uiFree;
    quint64 uiFree2Caller;

    DriveInfo()
    {
        sDriName = "";
        uiTotal = 0;
        uiFree = 0;
        uiFree2Caller = 0;
    }
};

// 获取所有本地磁盘的使用详情
QVector<DriveInfo> SystemInfo::get_local_drive_info()
{
    QVector<DriveInfo> vecDriInfo;

    DWORD diskCntArr = GetLogicalDrives();
    int diskCnt = 0;
    while (diskCntArr)
    {
        if (diskCntArr & 1)
        {
            diskCnt++;
        }
        diskCntArr = diskCntArr >> 1;
    }

    int DSLength = GetLogicalDriveStrings(0, NULL);
    TCHAR* DStr = new TCHAR[DSLength];
    GetLogicalDriveStrings(DSLength, (LPTSTR)DStr);

    int dri_type;
    BOOL fResult;
    quint64 iFree2Caller = 0;
    quint64 iTotalBytes = 0;
    quint64 iFreeBytes = 0;
    TCHAR *lpDriveStr = DStr;

    for (int i = 0; i < diskCnt; ++i)
    {
        dri_type = GetDriveType(lpDriveStr);
        if (dri_type == DRIVE_FIXED)
        {
            fResult = GetDiskFreeSpaceEx(lpDriveStr, (PULARGE_INTEGER)&iFree2Caller, (PULARGE_INTEGER)&iTotalBytes, (PULARGE_INTEGER)&iFreeBytes);
            if (fResult)
            {
                DriveInfo info;
                info.sDriName = QString::fromStdWString(lpDriveStr);
                info.uiFree2Caller = iFree2Caller;
                info.uiTotal = iTotalBytes;
                info.uiFree = iFreeBytes;

                vecDriInfo << info;
            }
            else
            {
                // 打印日志 megloglib
                qDebug() << "读取硬盘{0}使用情况时错误";
            }

            lpDriveStr += 4;
        }
        else
        {
            lpDriveStr += 4;
        }
    }

    delete[] DStr;
    DStr = NULL;

    return vecDriInfo;
}

 

本文标题为:【Windows系统】C++ Qt方式获取所有磁盘使用详情

基础教程推荐