获取磁盘总空间和剩余空间

Posted 晴天224

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取磁盘总空间和剩余空间相关的知识,希望对你有一定的参考价值。

BOOL MyLocalMacIP::GetSpaceInfo(CString &totalspace, CString &usedspace)  
{  
    long long total = 0;  
    long long used = 0;  
  
    ::vector<CString> drivernames;  
    int drivernum = 0;  
  
    CString str;  
  
    DWORD size = ::GetLogicalDriveStringsA(0, NULL);  
    if (0 != size)  
    {  
        HANDLE heap = ::GetProcessHeap();  
        LPSTR lp = (LPSTR)HeapAlloc(heap, HEAP_ZERO_MEMORY, size*sizeof(TCHAR));  
        ::GetLogicalDriveStringsA(size*sizeof(TCHAR), lp);  
  
        while(0 != *lp )  
        {  
            /* we don‘t need C:*/  
            int str_num = strcmp((char*)lp, ("C:\\"));  
            if (str_num == 0)  
            {  
                lp = strchr(lp, 0)+1;  
                continue;  
            }  
            wchar_t* lp_buffer = conversion.CharToWchar(lp);  
            UINT res = ::GetDriveTypeW(lp_buffer);  
            if(DRIVE_FIXED == res)  
            {  
                CString str = lp;  
                drivernames.push_back(str);  
                drivernum++;  
            }  
            delete []lp_buffer;  
            lp = strchr(lp, 0)+1;  
      
        }  
  
    }  
    ULARGE_INTEGER FreeSpace, CallSpace, TotalSpace;  
      
    for (int i = 0; i < drivernum; i++)  
    {  
        ::GetDiskFreeSpaceEx(drivernames[i], &FreeSpace, &CallSpace, &TotalSpace);  
         total += CallSpace.QuadPart;  
         used += FreeSpace.QuadPart;  
    }  
  
    string stotal = conversion.IntToString(total);  
    string sused = conversion.IntToString(used);  
    totalspace = CString(stotal.c_str());  
    usedspace = CString(sused.c_str());  
  
    return TRUE;  
} 

  

  1. BOOL MyLocalMacIP::GetSpaceInfo(CString &totalspace, CString &usedspace)  
  2. {  
  3.     long long total = 0;  
  4.     long long used = 0;  
  5.   
  6.     ::vector<CString> drivernames;  
  7.     int drivernum = 0;  
  8.   
  9.     CString str;  
  10.   
  11.     DWORD size = ::GetLogicalDriveStringsA(0, NULL);  
  12.     if (0 != size)  
  13.     {  
  14.         HANDLE heap = ::GetProcessHeap();  
  15.         LPSTR lp = (LPSTR)HeapAlloc(heap, HEAP_ZERO_MEMORY, size*sizeof(TCHAR));  
  16.         ::GetLogicalDriveStringsA(size*sizeof(TCHAR), lp);  
  17.   
  18.         while(0 != *lp )  
  19.         {  
  20.             /* we don‘t need C:*/  
  21.             int str_num = strcmp((char*)lp, ("C:\\"));  
  22.             if (str_num == 0)  
  23.             {  
  24.                 lp = strchr(lp, 0)+1;  
  25.                 continue;  
  26.             }  
  27.             wchar_t* lp_buffer = conversion.CharToWchar(lp);  
  28.             UINT res = ::GetDriveTypeW(lp_buffer);  
  29.             if(DRIVE_FIXED == res)  
  30.             {  
  31.                 CString str = lp;  
  32.                 drivernames.push_back(str);  
  33.                 drivernum++;  
  34.             }  
  35.             delete []lp_buffer;  
  36.             lp = strchr(lp, 0)+1;  
  37.       
  38.         }  
  39.   
  40.     }  
  41.     ULARGE_INTEGER FreeSpace, CallSpace, TotalSpace;  
  42.       
  43.     for (int i = 0; i < drivernum; i++)  
  44.     {  
  45.         ::GetDiskFreeSpaceEx(drivernames[i], &FreeSpace, &CallSpace, &TotalSpace);  
  46.          total += CallSpace.QuadPart;  
  47.          used += FreeSpace.QuadPart;  
  48.     }  
  49.   
  50.     string stotal = conversion.IntToString(total);  
  51.     string sused = conversion.IntToString(used);  
  52.     totalspace = CString(stotal.c_str());  
  53.     usedspace = CString(sused.c_str());  
  54.   
  55.     return TRUE;  

以上是关于获取磁盘总空间和剩余空间的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server--获取磁盘空间使用情况

ORACLE ASM中查询表空间使用情况数据文件路径裸设备磁盘总大小剩余大小

Android获取外部和内部存储空间总大小

linux查看磁盘空间 命令

linux中怎么查看硬盘剩余空间

使用 Prometheus 获取总磁盘空间和可用磁盘空间