可对目录进行监控,然后重读文件的信息,以下是对e:\temp目录监控的程序 procedure TForm1.Button1Click(Sender: TObject); var stat:DWORD; hd:THANDLE; begin hd:=FindFirstChangeNotification('e:\temp',false,FILE_NOTIFY_CHANGE_FILE_NAME); if hd=INVALID_HANDLE_VALUE then begin ShowMessage( Syserrormessage(getlasterror())); Exit; end; while true do begin stat:=WaitForSingleObject(hd,$ffffffff); if stat =0 then begin ShowMessage('有文件改变'); FindCloseChangeNotification(hd); //关闭句柄 exit; end; end;end;
FindFirstChangeNotification
FindNextChangeNotification
或者
ReadDirectoryChangeW来实现
procedure TForm1.Button1Click(Sender: TObject);
var
stat:DWORD;
hd:THANDLE;
begin
hd:=FindFirstChangeNotification('e:\temp',false,FILE_NOTIFY_CHANGE_FILE_NAME);
if hd=INVALID_HANDLE_VALUE then
begin
ShowMessage( Syserrormessage(getlasterror()));
Exit;
end;
while true do
begin
stat:=WaitForSingleObject(hd,$ffffffff);
if stat =0 then
begin
ShowMessage('有文件改变');
FindCloseChangeNotification(hd); //关闭句柄
exit;
end;
end;end;