FindFirstChangeNotification的用法是这样的: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;
Root设置要监测的目录;
WatchSubTree设置是否要监测子目录;
NotifyFilters设置要监测的变化事件:
nfFileNameChange - 文件名改变
nfDirNameChange - 目录名改变
nfAttributeChange - 属性改变
nfSizeChange - 大小改变
nfWriteChange - 写入
nfSecurityChange - 安全性改变(可能是指NT/2000/XP上的安全性吧)
只要控件监测到你设置的变化,就会产生OnChange事件。
另外能不能得到被改变的文件、文件夹的名称。
难道要再遍历一边文件列表才能知道是哪里变化了吗?
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;
http://www.fltvu.com/jiaocheng/chenxu1/DELPHI/vcl/file/discmon.zip
see MSDN for more info
问题还是没解决啊,
要支持所有windows系统
scroll down to "How FileMon Works"