📄 space.cpp
字号:
/*
** **************************************************************************
**
**
** (c) CopyRight 2006-2006, ZheJiang Dahua Technology Stock Co.Ltd.
** All Rights Reserved
**
** File Name : Space.cpp
** Description : 硬盘空间清理
** Modification : 2006/8/15
**
** Current ver : 1.0.0
** Writer : 金杰
**
** **************************************************************************
*/
#include "stdafx.h"
#include "ClearDisk.h"
#include "Space.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSpace::CSpace()
{
m_strPath.Empty();
m_strDriver.Empty();
m_dwFreeSpace = 0;
m_dwThreadId = 0;
m_hMsgWnd = 0;
}
CSpace::~CSpace()
{
}
/*
== ============================================================
== Function : SetMsgWnd
== Description : 设置接受消息得窗口
== Argument :
==
== Return :
==
== Modification : 2006-08-15
== Writer : 金杰
== ============================================================
*/
void CSpace::SetMsgWnd( HWND hWnd )
{
if( hWnd )
{
m_hMsgWnd = hWnd;
}
else
{
m_hMsgWnd = NULL;
}
}
/*
== ============================================================
== Function : SetFreeSpace
== Description : 设置空闲空间的大小范围
== Argument :
==
== Return :
==
== Modification : 2006-08-15
== Writer : 金杰
== ============================================================
*/
void CSpace::SetFreeSpace( DWORD dwSpace )
{
if (dwSpace > 0)
{
m_dwFreeSpace = dwSpace;
}
else
{
m_dwFreeSpace = 0;
}
}
/*
== ============================================================
== Function : SetPath
== Description : 设置检测路径
== Argument :
==
== Return :
==
== Modification : 2006-08-15
== Writer : 金杰
== ============================================================
*/
void CSpace::SetPath( CString strPath )
{
if (strPath.GetLength() > 0)
{
if( strPath.Find( ":" ) < 1 )
{
return;
}
m_strPath = strPath;
m_strDriver = strPath.Left( strPath.Find( ":" ) + 1 ) ;
}
else
{
m_strDriver.Empty();
m_strPath.Empty();
}
}
/*
== ============================================================
== Function : PostWndMsg
== Description : 通知主程序发送警告信息
== Argument :
==
== Return :
==
== Modification : 2006-08-15
== Writer : 金杰
== ============================================================
*/
void CSpace::PostWndMsg( )
{
if( m_hMsgWnd )
{
::PostMessage( m_hMsgWnd, WM_ALARMMSG, NULL, NULL );
}
}
/*
== ============================================================
== Function : IsFull
== Description : 判断磁盘可用空间是否不足
== Argument :
==
== Return : TRUE : 空间已满
== FALSE : 空间未满
== Modification : 2006-08-15
== Writer : 金杰
== ============================================================
*/
BOOL CSpace::IsFull( )
{
if( m_strDriver.GetLength() < 1 )
{
return TRUE;
}
BOOL fResult;
unsigned _int64 i64FreeBytesToCaller;
unsigned _int64 i64TotalBytes;
unsigned _int64 i64FreeBytes;
fResult = GetDiskFreeSpaceEx ( m_strDriver,
(PULARGE_INTEGER)&i64FreeBytesToCaller,
(PULARGE_INTEGER)&i64TotalBytes,
(PULARGE_INTEGER)&i64FreeBytes );
if( fResult )
{
i64TotalBytes = i64TotalBytes / 1024 / 1024;
i64FreeBytes = i64FreeBytes / 1024 / 1024;
CString str;
str.Format( "磁盘大小=%d M", i64TotalBytes );
//AfxMessageBox( str );
str.Format( "剩余空间 = %d M", i64FreeBytes );
//AfxMessageBox( str );
if ( m_dwFreeSpace > i64FreeBytes )
{
return TRUE;
}
}
return FALSE;
}
/*
== ============================================================
== Function : IsEnough
== Description : 在清除文件后判断可用空间是否足够
== Argument :
==
== Return : TRUE : 足够
== FALSE : 不足
== Modification : 2006-08-15
== Writer : 金杰
== ============================================================
*/
BOOL CSpace::IsEnough( )
{
if( m_strDriver.GetLength() < 1 )
return TRUE;
BOOL fResult;
unsigned _int64 i64FreeBytesToCaller;
unsigned _int64 i64TotalBytes;
unsigned _int64 i64FreeBytes;
fResult = GetDiskFreeSpaceEx ( m_strDriver,
(PULARGE_INTEGER)&i64FreeBytesToCaller,
(PULARGE_INTEGER)&i64TotalBytes,
(PULARGE_INTEGER)&i64FreeBytes );
if( fResult )
{
i64TotalBytes = i64TotalBytes / 1024 / 1024;
i64FreeBytes = i64FreeBytes / 1024 / 1024;
CString str;
str.Format( "磁盘大小=%d M", i64TotalBytes );
//AfxMessageBox( str );
str.Format( "剩余空间 = %d M", i64FreeBytes );
//AfxMessageBox( str );
if ( m_dwFreeSpace + 1000 < i64FreeBytes )
{
return TRUE;
}
}
return FALSE;
}
/*
== ============================================================
== Function : Clear
== Description : 清除文件
== Argument :
==
== Return : TRUE : 清除
== FALSE : 未清除
== Modification : 2006-08-15
== Writer : 金杰
== ============================================================
*/
BOOL CSpace::Clear( )
{
if( m_strPath.GetLength() < 1 )
{
return FALSE;
}
int nDelCnt = 0;
CString strFile;
STRFILE *pSort = NULL;
POSITION pos, posbuf;
pos = NULL;
posbuf = NULL;
//读取文件清单
if( m_lzFile.GetCount() < 1 )
{
ListFile( );
}
//通知主程序发送警告信息
PostWndMsg( );
//删除历史文件
for( pos = m_lzFile.GetHeadPosition(); pos; )
{
posbuf = pos;
pSort = (PSTRFILE)m_lzFile.GetNext( pos );
if( !pSort )
{
break;
}
strFile.Format( "%s\\%s", m_strPath, pSort->filename );
if( DeleteFile( strFile ) )
{
m_lzFile.RemoveAt( posbuf );
delete pSort;
pSort = NULL;
nDelCnt ++;
}
//删除文件后再判断磁盘可用空间是否充足
//每删除10个文件进行一次判断
if( nDelCnt > 10 )
{
if( IsEnough ( ) )
{
break;
}
nDelCnt = 0;
}
}
return TRUE;
}
/*
== ============================================================
== Function : ListFile
== Description : 读取文件清单,并按照时间进行排序
== Argument :
==
== Return : TRUE :读取成功
==
== Modification : 2006-08-15
== Writer : 金杰
== ============================================================
*/
BOOL CSpace::ListFile( )
{
CString strBuf;
WIN32_FIND_DATA c_file;
HANDLE hFile;
if( (hFile = FindFirstFile( m_strPath + "\\*.*", &c_file )) != INVALID_HANDLE_VALUE )
{
do
{
char* pFileName = new char[51];
memset( pFileName, 0, 51 );
strBuf.Format( "%s\\%s", m_strPath, c_file.cFileName );
if( !strcmp( c_file.cFileName, "." ) || !strcmp( c_file.cFileName, ".." ) )
{
continue;
}
if( c_file.dwFileAttributes == 16 ) //目录
{
//ClearDisk( strBuf );
//RemoveDirectory( strBuf );
}
else //文件
{
//DeleteFile( strBuf );
SortFile( c_file );
}
memset( &c_file, 0, sizeof(WIN32_FIND_DATA) );
}
while( FindNextFile(hFile,&c_file) );
}
FindClose(hFile);
return TRUE;
}
/*
== ============================================================
== Function : SortFile
== Description : 对加入到文件列表的文件进行排序
== Argument :
==
== Return : TRUE :操作成功
==
== Modification : 2006-08-15
== Writer : 金杰
== ============================================================
*/
BOOL CSpace::SortFile( WIN32_FIND_DATA cfile )
{
POSITION pos = NULL;
POSITION posbuf = NULL;
STRFILE *pSort = NULL;
STRFILE *pFile = new STRFILE;
memset( pFile, 0, sizeof( STRFILE ) );
strcpy( pFile->filename, cfile.cFileName );
pFile->ftLastWriteTime.dwHighDateTime = cfile.ftLastWriteTime.dwHighDateTime;
pFile->ftLastWriteTime.dwLowDateTime = cfile.ftLastWriteTime.dwLowDateTime;
if( m_lzFile.GetCount() == 0 )
{
m_lzFile.AddTail( (CObject *)pFile );
}
else
{
for( pos = m_lzFile.GetHeadPosition(); pos; )
{
posbuf = pos;
pSort = (STRFILE *)m_lzFile.GetNext( pos );
if( !pSort )
{
m_lzFile.AddTail( (CObject *)pFile );
break;
}
if( pFile->ftLastWriteTime.dwHighDateTime < pSort->ftLastWriteTime.dwHighDateTime )
{
m_lzFile.InsertBefore( posbuf, (CObject *)pFile );
break;
}
else if( pFile->ftLastWriteTime.dwHighDateTime == pSort->ftLastWriteTime.dwHighDateTime )
{
if( pFile->ftLastWriteTime.dwLowDateTime < pSort->ftLastWriteTime.dwLowDateTime )
{
m_lzFile.InsertBefore( posbuf, (CObject *)pFile );
break;
}
}
else if( !pos )
{
m_lzFile.AddTail( (CObject *)pFile );
break;
}
}
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -