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, szExePath, 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: ext.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

原文地址:https://www.cnblogs.com/liuruoqian/p/13023943.html