1. 程式人生 > WINDOWS開發 >windows 之磁碟空間剩餘空間監測

windows 之磁碟空間剩餘空間監測

監測軟體當前目錄的磁碟剩餘量是否達到1GB

bool SdLoginManager::checkDiskCapacity()
{
  bool res = true;
  BOOL fResult;
  unsigned _int64 i64FreeBytesToCaller;
  unsigned _int64 i64TotalBytes;
  unsigned _int64 i64FreeBytes;
  int freespace = 0;
  float totalspace = 0;
  wchar_t szExePath[MAX_PATH] = { 0 };

  GetModuleFileNameW

(NULL,szExePath,MAX_PATH);//獲取當前程序所在路徑擷取磁碟路徑即可
  wchar_t rootPaht[64] = {0};

  if (lstrlen(szExePath) <= 0)
  {
    SdLog("get the curentdir with error %d",GetLastError());
    return res;
  }

  wcsncpy(rootPaht,3);

  fResult = GetDiskFreeSpaceEx(rootPaht,                //獲取磁碟剩餘空間大小,總大小.
                (PULARGE_INTEGER)&i64FreeBytesToCaller,

                (PULARGE_INTEGER)&i64TotalBytes,
                (PULARGE_INTEGER)&i64FreeBytes);

  if (fResult)
  {
    totalspace = (float)i64TotalBytes / 1024 / 1024 / 1024;
    freespace = (float)i64FreeBytes / 1024 / 1024 / 1024;
  }
  if (!freespace)
  {
    res = false;
  }

  return res;
 }

以上結合cmd:> fsutil file createnew E:\text.txt 1024000000

上述命令生成1GB txt具體大小自己定義,然後進行測試,別生成在C盤即可.

具體api詳情轉ms手冊:

https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulefilenamew

https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getdiskfreespaceexa