C++ 监测磁盘空间
硬盘管理器
头文件 HardDiskManager.h :
#if _MSC_VER > 1000 #pragma once #endif #include <windows.h> class CHardDiskManager { public: CHardDiskManager(); virtual ~CHardDiskManager(); bool CheckFreeSpace(LPCTSTR lpDirectoryName); DWORD64 GetFreeBytesAvailable(void); DWORD64 GetTotalNumberOfBytes(void); DWORD64 GetTotalNumberOfFreeBytes(void); double GetFreeGBytesAvailable(void); double GetTotalNumberOfGBytes(void); double GetTotalNumberOfFreeGBytes(void); private: ULARGE_INTEGER m_uliFreeBytesAvailable; ULARGE_INTEGER m_uliTotalNumberOfBytes; ULARGE_INTEGER m_uliTotalNumberOfFreeBytes; };
_MSC_VER: 用来定义当前微软公司自己的编译器的主版本
MS VC++ 14.0 _MSC_VER = 1900 (Visual Studio 2015)
MS VC++ 12.0 _MSC_VER = 1800 (VisualStudio 2013)
MS VC++ 11.0 _MSC_VER = 1700 (VisualStudio 2012)
MS VC++ 10.0 _MSC_VER = 1600(VisualStudio 2010)
MS VC++ 9.0 _MSC_VER = 1500(VisualStudio 2008)
MS VC++ 8.0 _MSC_VER = 1400(VisualStudio 2005)
MS VC++ 7.1 _MSC_VER = 1310(VisualStudio 2003)
MS VC++ 7.0 _MSC_VER = 1300(VisualStudio .NET)
MS VC++ 6.0 _MSC_VER = 1200(VisualStudio 98)
MS VC++ 5.0 _MSC_VER = 1100(VisualStudio 97)
ULARGE_INTEGER:一个64位的无符号整型值
源文件 HardDiskManager.cpp :
// HardDiskManager.cpp #include <afxwin.h> #include "HardDiskManager.h" CHardDiskManager::CHardDiskManager() { m_uliFreeBytesAvailable.QuadPart = 0L; m_uliTotalNumberOfBytes.QuadPart = 0L; m_uliTotalNumberOfFreeBytes.QuadPart = 0L; } CHardDiskManager::~CHardDiskManager() { } bool CHardDiskManager::CheckFreeSpace(LPCTSTR lpDirectoryName) { if( !GetDiskFreeSpaceEx( lpDirectoryName, &m_uliFreeBytesAvailable, &m_uliTotalNumberOfBytes, &m_uliTotalNumberOfFreeBytes) ) return false; return true; } DWORD64 CHardDiskManager::GetFreeBytesAvailable(void) { return m_uliFreeBytesAvailable.QuadPart; } DWORD64 CHardDiskManager::GetTotalNumberOfBytes(void) { return m_uliTotalNumberOfBytes.QuadPart; } DWORD64 CHardDiskManager::GetTotalNumberOfFreeBytes(void) { return m_uliTotalNumberOfFreeBytes.QuadPart; } double CHardDiskManager::GetFreeGBytesAvailable(void) { return (double)( (signed __int64)(m_uliFreeBytesAvailable.QuadPart)/1073741824 ); } double CHardDiskManager::GetTotalNumberOfGBytes(void) { return (double)( (signed __int64)(m_uliTotalNumberOfBytes.QuadPart)/1073741824 ); } double CHardDiskManager::GetTotalNumberOfFreeGBytes(void) { return (double)( (signed __int64)(m_uliTotalNumberOfFreeBytes.QuadPart)/1073741824 ); }
LPCTSTR用来表示你的字符是否使用UNICODE, 如果你的程序定义了UNICODE或者其他相关的宏,那么这个字符或者字符串将被作为UNICODE字符串,否则就是标准的ANSI字符串。
DWORD全称Double Word,是指注册表的键值,每个word为2个字节的长度,DWORD 双字即为4个字节,每个字节是8位,共32位。
DWORD64 无符号整型值。
调用:
// HardDisk.cpp : Defines the entry point for the console application. #include "stdafx.h" #include <iostream> #include "HardDiskManager.h" #include <tchar.h> using namespace std; int main(int argc, char* argv[]) { CHardDiskManager manager; manager.CheckFreeSpace(_T("c:")); cout << "Drive C:\\" << endl; cout << "FreeBytesAvailable: " << manager.GetFreeGBytesAvailable() << " GBytes. " << endl; cout << "TotalNumberOfBytes: " << manager.GetTotalNumberOfGBytes() << " GBytes. " << endl; cout << "TotalNumberOfFreeBytes: " << manager.GetTotalNumberOfFreeGBytes() << " GBytes. " << endl; manager.CheckFreeSpace(_T("d:")); cout << "Drive D:\\" << endl; cout << "FreeBytesAvailable: " << manager.GetFreeGBytesAvailable() << " GBytes. " << endl; cout << "TotalNumberOfBytes: " << manager.GetTotalNumberOfGBytes() << " GBytes. " << endl; cout << "TotalNumberOfFreeBytes: " << manager.GetTotalNumberOfFreeGBytes() << " GBytes. " << endl; return 0; }
基本翻译
abbr. 双字值(Double Word)