当前位置:编程学习 > Delphi >>

监控指定文件夹

该功能在delphi XE + XP 下测试通过
 
O2DirSpy.pas    (该单元获取自网络)
[delphi]  
{====================================================================}  
{   TOxygenDirectorySpy Component, v1.6 c 2000-2001 Oxygen Software  }  
{--------------------------------------------------------------------}  
{          Written by Oleg Fyodorov, delphi@oxygensoftware.com       }  
{                  http://www.oxygensoftware.com                     }  
{====================================================================}  
  
unit O2DirSpy;  
  
interface  
  
  uses Classes, Controls, Windows, SysUtils, ShellApi, Dialogs, Messages, FileCtrl;  
  
  type  
    TDirectoryChangeType = (ctNone, ctAttributes, ctSize, ctCreationTime, ctLastModificationTime, ctLastAccessTime, ctLastTime, ctCreate, ctRemove);  
  
    TOxygenDirectorySpy = class;  
  
    TDirectoryChangeRecord = record  
      Directory : String;  
      FileFlag : Boolean; // When True, ChangeType applies to a file; False - ChangeType applies to Directory  
      Name : String; // Name of changed file/directory  
      OldTime, NewTime : TDateTime;  // Significant only when ChangeType is one of ctCreationTime, ctLastModificationTime, ctLastAccessTime, ctLastTime  
      OldAttributes, NewAttributes : DWord; // Significant only when ChangeType is ctAttributes  
      OldSize, NewSize : DWord; // Significant only when ChangeType is ctSize  
      ChangeType : TDirectoryChangeType; // Describes a change type (creation, removing etc.)  
    end;  
  
    TSpySearchRec = record  
      Time: Integer;  
      Size: Integer;  
      Attr: Integer;  
      dwFileAttributes: DWORD;  
      ftCreationTime: TFileTime;  
      ftLastAccessTime: TFileTime;  
      ftLastWriteTime: TFileTime;  
      nFileSizeHigh: DWORD;  
      nFileSizeLow: DWORD;  
    end;  
  
    TFileData = class  
      private  
        FSearchRec : TSpySearchRec;  
        Name: TFileName;  
        FFound : Boolean;  
      public  
        constructor Create;  
        procedure Free;  
    end;  
  
    TFileDataList = class(TStringList)  
      private  
        function NewFileData(const FileName : String; sr : TSearchRec) : TFileData;  
        function GetFoundCount : Integer;  
      public  
        property FoundCount : Integer read GetFoundCount;  
  
        destructor Destroy; override;  
        function AddFileData(FileData : TFileData) : Integer;  
        function AddSearchRec(const Directory : String; sr : TSearchRec) : Integer;  
        procedure Delete(Index : Integer); override;  
        procedure Clear; override;  
        procedure SetFound(Value : Boolean);  
    end;  
  
    TReadDirChangesThread = class(TThread)  
    private  
      FOwner           : TOxygenDirectorySpy;  
      FDirectories     : TStringList;  
      FHandles         : TList;  
      FChangeRecord    : TDirectoryChangeRecord;  
      FFilesData,  
      FTempFilesData   : TFileDataList;  
      pHandles         : PWOHandleArray;  
      procedure ReleaseHandle;  
      procedure AllocateHandle;  
      procedure ReadDirectories(DestData : TFileDataList);  
      procedure CompareSearchRec(var srOld, srNew : TSpySearchRec);  
    protected  
      procedure Execute; override;  
      procedure Notify;  
    public  
      constructor Create(Owner : TOxygenDirectorySpy);  
      destructor Destroy; override;  
      procedure Reset;  
    end;  
  
    TChangeDirectoryEvent = procedure (Sender : TObject; ChangeRecord : TDirectoryChangeRecord) of object;  
  
    TOxygenDirectorySpy = class(TComponent)  
      private  
        FThread : TReadDirChangesThread;  
        FEnabled,  
        FWatchSubTree : Boolean;  
        FDirectories : TStrings;  
        FOnChangeDirectory : TChangeDirectoryEvent;  
  
        procedure SetEnabled(const Value : Boolean);  
        procedure CheckDirectories;  
        procedure SetDirectories(const Value : TStrings);  
        procedure SetWatchSubTree(const Value : Boolean);  
      protected  
        procedure DoChangeDirectory(ChangeRecord : TDirectoryChangeRecord);  
      published  
        property Enabled : Boolean read FEnabled write SetEnabled;  
        property Directories : TStrings read FDirectories write SetDirectories;  
        property WatchSubTree : Boolean read FWatchSubTree write SetWatchSubTree;  
        property OnChangeDirectory : TChangeDirectoryEvent read FOnChangeDirectory write FOnChangeDirectory;  
      public  
        constructor Create(AOwner : TComponent);
补充:软件开发 , Delphi ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,