Windows下监控文件夹或文件变化
  WtpFkP2ympcR 2023年11月02日 50 0


API函数

Windows提供了监控文件夹或文件变化的API, 如下:
FindFirstChangeNotification
FindCloseChangeNotification
FindNextChangeNotification
ReadDirectoryChangesW

具体使用方式,查看msdn或网上找资料
也可参看博文:

监控文件夹变化(新建、删除、重命名等)例子

#include <windows.h>
#include <iostream>
#include <tchar.h>

int main(int argc, char** argv )
{
	HANDLE hWatchDir = ::FindFirstChangeNotification(_T("F:\\testaaa"),
		TRUE, FILE_NOTIFY_CHANGE_DIR_NAME);
	if (hWatchDir == INVALID_HANDLE_VALUE)
	{
		std::cerr << "invalid watch handle with error " 
			<< ::GetLastError() << std::endl;
		return 1;
	}

	while (TRUE)
	{
		std::cout << "waiting dir changing..." << std::endl;
		if (WAIT_OBJECT_0 == ::WaitForSingleObject(hWatchDir, INFINITE))
		{
			std::cout << "dir or sub dir changed" << std::endl;
		}
		else
		{
			std::cerr << "wait occur error " << ::GetLastError() << std::endl;
			break;
		}

		if (!::FindNextChangeNotification(hWatchDir))
		{
			std::cerr << "find next change failed with error " << ::GetLastError()
				<< std::endl;
			break;
		}
	}

	::FindCloseChangeNotification(hWatchDir);
	hWatchDir = NULL;

	return 0;
}


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  anLrwkgbyYZS   2023年12月30日   28   0   0 i++iosi++ioscici
WtpFkP2ympcR