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;
}