Using Delphi to track changes made to folder?

人走茶凉 提交于 2019-12-22 05:07:12

问题


I need to writing a delphi program which will monitor a folder for changes (Add, update, rename and removal of files). Is the TShellChangeNotifier is way to go? To be honest, I have no idea how to use it.

Please help.

Thanks.


回答1:


This question might help. mghie's answer shows how to properly use ReadDirectoryChangesW.




回答2:


I think this article will help you: Monitoring System Shell Changes using Delphi

Basically it analyzes the TShellChangeNotifier, discards it and then goes for a TSHChangeNotify which is basically a wrapper for the SHChangeNotify windows api function.




回答3:


i suggest using madShell

RegisterShellEvent(ShellEvent, pathToMonitor, false, [seItemCreated, seItemRenamed]);

//

procedure Tform.ShellEvent(event: TShellEventType; const obj1, obj2: IShellObj; drive: char; value: cardinal);
var
  filename: string;
  isReady: boolean;
begin
  if (event = seItemCreated) then
    filename := obj1.Path
  else if (event = seItemRenamed) then
    filename := obj2.Path
  else
    exit;

  // try to open to ensure it's read for reading
  repeat
    try
      TfileStream.Create(filename, fmOpenRead + fmShareExclusive).Free;
      isReady := true;
    except
      isReady := false;
      sleep(250);
    end;
  until (isReady) or (not FileExists(filename));

  OutputDebugString(pChar('ShellEvent: ' + filename));

end;


来源:https://stackoverflow.com/questions/1747152/using-delphi-to-track-changes-made-to-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!