vc++ 监控指定路径下文件变化

参考MSDN文档

https://docs.microsoft.com/zh-cn/windows/desktop/api/winbase/nf-winbase-readdirectorychangesw

https://docs.microsoft.com/zh-cn/windows/desktop/api/winnt/ns-winnt-_file_notify_information

 

具体看代码

 # include < iostream >
 # include < windows.h >
 # include < process.h >
 # include < tchar.h >
 # include < string >
using namespace std;
typedef void( * CHANGESCALLBACK)(const std::wstring strFileName);
/*
@ 监控指定目录下文件变化
@ strDirPath 需要监控的目录
@ dwChangesType 需要监控文件变化方式
@ onCallBack  变化后的回调处理函数
 */
void CheckFilesChanges(const std::wstring strDirPath, DWORD dwChangesType, CHANGESCALLBACK onCallBack);
void PrintMsg(const std::wstring strFileName) {
	wprintf_s(L "new add File:\t %s\r\n", strFileName.c_str());
}
void CheckFilesChanges(const std::wstring strDirPath, DWORD dwChangesType, CHANGESCALLBACK onCallBack) {
	if (strDirPath.empty() || !onCallBack)
		return;
	HANDLE hDir = INVALID_HANDLE_VALUE;
	BYTE lpBuffer[1024];
	ZeroMemory(lpBuffer, 1024);
	DWORD cbBytes = NULL;
	BOOL isOk = FALSE;
	FILE_NOTIFY_INFORMATION * pnotify = (FILE_NOTIFY_INFORMATION * )lpBuffer;
	FILE_NOTIFY_INFORMATION * tmp;
	ZeroMemory( & lpBuffer, sizeof(FILE_NOTIFY_INFORMATION));
	hDir = CreateFile(strDirPath.c_str(), FILE_LIST_DIRECTORY,
			FILE_SHARE_READ |
			FILE_SHARE_WRITE |
			FILE_SHARE_DELETE, NULL,
			OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS |
			FILE_FLAG_OVERLAPPED, NULL);
	if (INVALID_HANDLE_VALUE == hDir)
		return;
	while (true) {
		isOk = ReadDirectoryChangesW(hDir,  & lpBuffer,
				sizeof(lpBuffer),
				TRUE,
				FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE,
				 & cbBytes,
				NULL,
				NULL);
		if (isOk) {
			tmp = pnotify;
			PWSTR fileName = nullptr;
			std::wstring strFileName;
			if (tmp->FileNameLength) {
				DWORD dwLength = tmp->FileNameLength + 1;
				fileName = new wchar_t[dwLength];
				if (fileName) {
					memset(fileName, 0, dwLength * 2);
					memcpy(fileName, tmp->FileName, dwLength * 2);
					strFileName = fileName;
				}
				if (fileName)
					delete []fileName;
			}
			if (tmp->Action == dwChangesType) {
				onCallBack(strFileName);
			}
			ZeroMemory(tmp, 1024);
			/*switch (tmp->Action){
			case FILE_ACTION_ADDED:	//当前目录新增文件{
			onCallBack(strFileName);
			}break;

			case FILE_ACTION_REMOVED:	//该文件已从目录中删除{
			onCallBack(strFileName);
			}
			break;
			case FILE_ACTION_MODIFIED:	//该文件已被修改。也可以是时间戳或属性的更改{
			onCallBack(strFileName);
			}
			break;
			case FILE_ACTION_RENAMED_OLD_NAME:	//该文件已重命名,这是旧名称。{
			onCallBack(strFileName);
			}
			break;
			case FILE_ACTION_RENAMED_NEW_NAME: //该文件已重命名,这是新名称。{
			onCallBack(strFileName);
			}
			break;
			default:
			break;
			}*/
		}
	}
	CloseHandle(hDir);
}
unsigned int CALLBACK ThreadProc(void * arg) {
	CheckFilesChanges(_T("E:\\GoCode"), FILE_ACTION_ADDED, PrintMsg);
	return 0;
}
int main(int argc, char * argv[]) {
	HANDLE hThread = NULL;
	hThread = (HANDLE)::_beginthreadex(nullptr, NULL, ThreadProc, NULL, NULL, NULL);
	system("pause");
	return 0;
}

  

转载于:https://www.cnblogs.com/guolongzheng/p/9969746.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值