ReadDirectoryChangesW监控目录文件操作

// 目录监控多线程
UINT MonitorFileThreadProc(LPVOID lpVoid)
{
	char *pszDirectory = (char *)lpVoid;
	// 打开目录, 获取文件句柄
	HANDLE hDirectory = ::CreateFile(pszDirectory, FILE_LIST_DIRECTORY,
		FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
		FILE_FLAG_BACKUP_SEMANTICS, NULL);
	if (INVALID_HANDLE_VALUE == hDirectory)
	{
		ShowError("CreateFile");
		return 1;
	}
	char szTemp[MAX_PATH] = { 0 };
	BOOL bRet = FALSE;
	DWORD dwRet = 0;
	DWORD dwBufferSize = 2048;
	// 申请一个足够大的缓冲区 
	BYTE *pBuf = new BYTE[dwBufferSize];
	if (NULL == pBuf)
	{
		ShowError("new");
		return 2;
	}
	FILE_NOTIFY_INFORMATION *pFileNotifyInfo = (FILE_NOTIFY_INFORMATION *)pBuf;
	// 开始循环设置监控
	do
	{
		::RtlZeroMemory(pFileNotifyInfo, dwBufferSize);
		// 设置监控目录
		bRet = ::ReadDirectoryChangesW(hDirectory,
			pFileNotifyInfo,
			dwBufferSize,
			TRUE,
			FILE_NOTIFY_CHANGE_FILE_NAME |			// 修改文件名
			FILE_NOTIFY_CHANGE_ATTRIBUTES |			// 修改文件属性
			FILE_NOTIFY_CHANGE_LAST_WRITE,			// 最后一次写入
			&dwRet,
			NULL,
			NULL);
		if (FALSE == bRet)
		{
			ShowError("ReadDirectoryChangesW");
			break;
		}
		// 将宽字符转换成窄字符
		W2C((wchar_t *)(&pFileNotifyInfo->FileName), pFileNotifyInfo->FileNameLength, szTemp, MAX_PATH);
		// 判断操作类型并显示
		switch (pFileNotifyInfo->Action)
		{
			case FILE_ACTION_REMOVED:
				printf("[File Removed Action] %s\n", szTemp);
				break;
			case FILE_ACTION_ADDED:
			{
				// 新增文件
				printf("[File Added Action] %s\n", szTemp);
				break;
			}
			case FILE_ACTION_MODIFIED:
				printf("[File Modified Action] %s\n", szTemp);
				break;
			case FILE_ACTION_RENAMED_OLD_NAME:
				printf("[File Rename Action] %s\n", szTemp);
				break;
			case FILE_ACTION_RENAMED_NEW_NAME:
				printf("[File Rename Action] %s\n", szTemp);
				break;
			default:
			{
				break;
			}
		}
	} while (bRet);
	// 关闭句柄, 释放内存
	::CloseHandle(hDirectory);
	delete[] pBuf;
	pBuf = NULL;
	return 0;
}

你可能感兴趣的:(c++)