先用FindFirstChangeNotification登记要监测的文件夹 然后用WaitForSingleObject函数来等待文件变化事件的到来,并处理该时间 在处理后如果需要继续监测,那么还要 调用FindNextChangeNotification(…) 结束监视时要用FindCloseChangeNotification 这个要放到线程里面HANDLE FindFirstChangeNotification( LPCTSTR lpPathName, // pointer to name of directory to watch BOOL bWatchSubtree, // flag for monitoring directory or directory tree DWORD dwNotifyFilter // filter conditions to watch for ); 第1个参数指定路径 第2个为true就表示含子目录,false为当前目录 第3个是参数,有很多,你看下WIN32SDK 返回值通过调用WaitForSingleObject进行等待
随便写的,如果监视的目录没有变化,这个线程就一直假死,慎重操作uses QDialog;var S:WideString; H:THandle; begin if not SelectDirectory('','',S) then exit;H:=FindFirstChangeNotification(Pchar(String(S)),true,FILE_NOTIFY_CHANGE_FILE_NAME or FILE_NOTIFY_CHANGE_DIR_NAME); //监视改文件名或目录名 if WaitForSingleObject(H,INFINITE)=WAIT_OBJECT_0 then begin //监视等待 showmessage('有变化'); end; FindCloseChangeNotification(H); //结束监视 end;
然后用WaitForSingleObject函数来等待文件变化事件的到来,并处理该时间
在处理后如果需要继续监测,那么还要 调用FindNextChangeNotification(…)
结束监视时要用FindCloseChangeNotification
这个要放到线程里面HANDLE FindFirstChangeNotification(
LPCTSTR lpPathName, // pointer to name of directory to watch
BOOL bWatchSubtree, // flag for monitoring directory or directory tree
DWORD dwNotifyFilter // filter conditions to watch for
);
第1个参数指定路径
第2个为true就表示含子目录,false为当前目录
第3个是参数,有很多,你看下WIN32SDK
返回值通过调用WaitForSingleObject进行等待
S:WideString;
H:THandle;
begin
if not SelectDirectory('','',S) then exit;H:=FindFirstChangeNotification(Pchar(String(S)),true,FILE_NOTIFY_CHANGE_FILE_NAME or FILE_NOTIFY_CHANGE_DIR_NAME); //监视改文件名或目录名
if WaitForSingleObject(H,INFINITE)=WAIT_OBJECT_0 then begin //监视等待
showmessage('有变化');
end;
FindCloseChangeNotification(H); //结束监视
end;