I am writing a program in Delphi 7, that does jobs, which are defined in template files being located in the applications directory.
Not I wanted to be informed when a new template file in this directory is created. So I found your madShell and it works fine. Thanks for this.
But later, I noticed, that my program will also be notified, when files are created in sub directories although I did not indicated that.
Here is some code:
Code: Select all
// I have made a new class which handles your madShell
Type
TChangeType = (ctAll, ctFileCreated, ..., ctDirectoryRenamed);
TChangeTypes = Set Of TChangeType;
TDirectoryWatch = Class (TObject)
Private
fDirectory: String;
fSubTree : Boolean;
...
fOnFileCreated : TFileEvent;
...
procedure ShellEvent(event: TShellEventType; const obj1, obj2: IShellObj; drive: char; value: cardinal);
Protected
Procedure Initialize;
Procedure Finalize;
...
Procedure NotifyFileCreated (FileName: TFileName);
Public
Constructor Create (aPath: String; aSubTree: Boolean; aChangeTypes: TChangeTypes = [ctAll]); Overload;
...
Property ChangeTypes: TChangeTypes Read fChangeTypes Write fChangeTypes;
Property OnFileCreated: TFileEvent Read fOnFileCreated Write fOnFileCreated;
End;
implementation
Constructor TDirectoryWatch.Create(aPath: String; aSubTree: Boolean; aChangeTypes: TChangeTypes);
Begin
Inherited Create ();
fDirectory := IncludeTrailingPathDelimiter (aPath);
fChangeTypes := aChangeTypes;
fSubTree := aSubTree;
Initialize;
End;
procedure TDirectoryWatch.Finalize;
begin
UnregisterShellEvent (ShellEvent);
end;
procedure TDirectoryWatch.Initialize;
begin
RegisterShellEvent (ShellEvent, fDirectory, fSubTree);
end;
procedure TDirectoryWatch.NotifyFileCreated(FileName: TFileName);
begin
If ((ctFileCreated In ChangeTypes) Or (ctAll In ChangeTypes)) Then
If Assigned (fOnFileCreated) Then
fOnFileCreated (Self, FileName);
end;
procedure TDirectoryWatch.ShellEvent(event: TShellEventType; const obj1,
obj2: IShellObj; drive: char; value: cardinal);
begin
Case event Of
seItemCreated: NotifyFileCreated (obj1.Path);
...
End;
end;
In my program i create an instance of this class as follows:
Code: Select all
// Path is the applications directory:
// Path := ExtractFilePath (Application.ExeName);
fDirectoryWatch:= TDirectoryWatch.Create (Path, False);
Code: Select all
// fDirectory = 'D:\Sandbox\NavImport\Bin\'
// fSUbTree = False
RegisterShellEvent (ShellEvent, fDirectory, fSubTree);
Now being notified I can check whether the filepath is the same as the directories path, but I just want to ask, whether I made a mistake, or whether there is a bug in your madShell.
Greetings,
Muesli