我想用函数FindFirstChangeNotification实现监控 当文件夹里面有新的文件产生时就提示我去操作该文件, 我看了有C语言的例子不过没看过有delphi的例子 哪位大哥有delphi写的例子 提供一下           小弟谢谢了

解决方案 »

  1.   

    unit FileSysThread;interfaceuses
        Windows, SysUtils, Classes, comctrls;type
        TFileSysNotifyThread = class(TThread)
        private
            ErrCode: Integer;
            KillAddress: PInteger;
            NotifyHandle: THandle;
            WatchPath: String;
            WatchMask: Integer;
            procedure SignalFileNotification;
        protected
            procedure Execute; override;
        public
            constructor Create (const AWatchPath: String; AWatchMask: Integer; var Myself: TFileSysNotifyThread);
            destructor Destroy; override;
        end;implementationuses Dialogs;constructor TFileSysNotifyThread.Create (const AWatchPath: String; AWatchMask: Integer; var Myself: TFileSysNotifyThread);
    begin
        Inherited Create (True);
        WatchPath := AWatchPath;
        WatchMask := AWatchMask;
        KillAddress := Addr (Myself);
        Priority := tpLower;
        FreeOnTerminate := True;
        Suspended := False;
    end;destructor TFileSysNotifyThread.Destroy;
    begin
        if NotifyHandle <> THandle (-1) then
           FindCloseChangeNotification (NotifyHandle);
        Inherited Destroy;
        KillAddress^ := 0;
    end;procedure TFileSysNotifyThread.Execute;
    begin
        NotifyHandle := FindFirstChangeNotification (PChar (WatchPath), False, WatchMask);
        if NotifyHandle <> THandle (-1) then while not Terminated do begin
            ErrCode := WaitForSingleObject (NotifyHandle, 250);
            case ErrCode of
                 Wait_Timeout:
                     ;
                 Wait_Object_0:
                     begin
                         Synchronize (SignalFileNotification);
                         FindNextChangeNotification (NotifyHandle);
                     end;
                 else ;
            end;
        end;
    end;procedure TFileSysNotifyThread.SignalFileNotification;
    begin
        ShowMessage ('文件已改变!');
    end;end.