首页 > Windows开发 > 详细

windows 之磁盘空间剩余空间监测

时间:2020-06-01 12:28:14      阅读:70      评论:0      收藏:0      [点我收藏+]

监测软件当前目录的磁盘剩余量是否达到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:\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

windows 之磁盘空间剩余空间监测

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!