有没有办法感知一个目录内增加或者减少文件?(100分)

  • 主题发起人 主题发起人 silicon
  • 开始时间 开始时间
S

silicon

Unregistered / Unconfirmed
GUEST, unregistred user!
这个问题好象有点烦?
 
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *<br><br>Component: &nbsp; &nbsp;TDirMon<br>Description: &nbsp;This component encapsulate the Win 32 API function<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FindFirstChangeNotification. The primary purpose of this component<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; is to monitor changes in a given directory and fire an event<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; when a change occurs (file creation, deletion, ...).<br>Version: &nbsp; &nbsp; &nbsp;1.00<br>Created: &nbsp; &nbsp; &nbsp;March 27, 1997<br>Author: &nbsp; &nbsp; &nbsp; Fran&amp;ccedil;ois PIETTE<br>Email: &nbsp; &nbsp; &nbsp; &nbsp;francois.piette@ping.be &nbsp;http://www.rtfm.be/fpiette<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; francois.piette@f2202.n293.z2.fidonet.org<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2:293/2202@fidonet.org, BBS +32-4-3651395<br>Support: &nbsp; &nbsp; &nbsp;Please ask your question in the following newsgroup:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; news://forums.borland.com/borland.public.delphi.vcl.components.using<br>Legal issues: Copyright (C) 1997 by Fran&amp;ccedil;ois PIETTE &lt;francois.piette@ping.be&gt;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; This software is provided 'as-is', without any express or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; implied warranty. &nbsp;In no event will the author be held liable<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for any &nbsp;damages arising from the use of this software.<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Permission is granted to anyone to use this software for any<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; purpose, including commercial applications, and to alter it<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and redistribute it freely, subject to the following<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; restrictions:<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1. The origin of this software must not be misrepresented,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;you must not claim that you wrote the original software.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;If you use this software in a product, an acknowledgment <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;in the product documentation would be appreciated but is<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;not required.<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2. Altered source versions must be plainly marked as such, and <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;must not be misrepresented as being the original software.<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3. This notice may not be removed or altered from any source <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;distribution.<br><br>Modification History:<br>Aug 07, 1997 V1.0 Added some comments<br><br><br>&nbsp;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}<br>unit DirMon;<br><br>interface<br><br>uses<br>&nbsp; &nbsp; Windows,<br>&nbsp; &nbsp; SysUtils,<br>&nbsp; &nbsp; Messages,<br>&nbsp; &nbsp; Classes,<br>&nbsp; &nbsp; Graphics,<br>&nbsp; &nbsp; Controls,<br>&nbsp; &nbsp; Forms,<br>&nbsp; &nbsp; Dialogs,<br>&nbsp; &nbsp; Menus;<br><br>const<br>&nbsp; WM_DIRCHANGE= WM_USER + 1;<br><br>type<br>&nbsp; &nbsp; TDirMonValue = (dmcFileName,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dmcDirName,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dmcAttributes,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dmcSize,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dmcLastWrite,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dmcSecurity);<br>&nbsp; &nbsp; TDirMonType = set of TDirMonValue;<br><br>&nbsp; &nbsp; TDirMon = class(TComponent)<br>&nbsp; &nbsp; private<br>&nbsp; &nbsp; &nbsp; &nbsp; FDirectory &nbsp; &nbsp;: String;<br>&nbsp; &nbsp; &nbsp; &nbsp; FNotifyFilter : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; FWindowHandle : HWND;<br>&nbsp; &nbsp; &nbsp; &nbsp; FParamPtr &nbsp; &nbsp; : Pointer;<br>&nbsp; &nbsp; &nbsp; &nbsp; FMutexHandle &nbsp;: THandle;<br>&nbsp; &nbsp; &nbsp; &nbsp; FThreadHandle : THandle;<br>&nbsp; &nbsp; &nbsp; &nbsp; FThreadID &nbsp; &nbsp; : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; FOnDirChange &nbsp;: TNotifyEvent;<br>&nbsp; &nbsp; &nbsp; &nbsp; FOnStart &nbsp; &nbsp; &nbsp;: TNotifyEvent;<br>&nbsp; &nbsp; &nbsp; &nbsp; FOnStop &nbsp; &nbsp; &nbsp; : TNotifyEvent;<br>&nbsp; &nbsp; &nbsp; &nbsp; procedure WndProc(var MsgRec: TMessage);<br>&nbsp; &nbsp; &nbsp; &nbsp; procedure WMDirChange(var aMsg: TMessage); message WM_DIRCHANGE;<br>&nbsp; &nbsp; protected<br>&nbsp; &nbsp; &nbsp; &nbsp; procedure SetNotifyFilter(newValue : TDirMonType);<br>&nbsp; &nbsp; &nbsp; &nbsp; function &nbsp;GetNotifyFilter : TDirMonType;<br>&nbsp; &nbsp; &nbsp; &nbsp; procedure SetDirectory(newValue : String);<br>&nbsp; &nbsp; &nbsp; &nbsp; procedure TriggerDirChangeEvent; virtual;<br>&nbsp; &nbsp; &nbsp; &nbsp; procedure TriggerStartEvent; virtual;<br>&nbsp; &nbsp; &nbsp; &nbsp; procedure TriggerStopEvent; virtual;<br>&nbsp; &nbsp; public<br>&nbsp; &nbsp; &nbsp; &nbsp; constructor Create(AOwner : TComponent); override;<br>&nbsp; &nbsp; &nbsp; &nbsp; destructor &nbsp;Destroy; override;<br>&nbsp; &nbsp; &nbsp; &nbsp; procedure &nbsp; Start;<br>&nbsp; &nbsp; &nbsp; &nbsp; procedure &nbsp; Stop;<br>&nbsp; &nbsp; published<br>&nbsp; &nbsp; &nbsp; &nbsp; { Published properties and events }<br>&nbsp; &nbsp; &nbsp; &nbsp; property Directory : String &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; read &nbsp;FDirectory<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; write SetDirectory;<br>&nbsp; &nbsp; &nbsp; &nbsp; property NotifyFilter : TDirMonType read &nbsp;GetNotifyFilter<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; write SetNotifyFilter;<br>&nbsp; &nbsp; &nbsp; &nbsp; property OnDirChange : TNotifyEvent &nbsp; &nbsp; read &nbsp;FOnDirChange<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; write FOnDirChange;<br>&nbsp; &nbsp; &nbsp; &nbsp; property Handle : HWND &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;read &nbsp;FWindowHandle;<br>&nbsp; &nbsp; &nbsp; &nbsp; property OnStart : TNotifyEvent read FOnStart write FOnStart;<br>&nbsp; &nbsp; &nbsp; &nbsp; property OnStop : TNotifyEvent read FOnStop write FOnStop;<br>&nbsp; &nbsp; end;<br><br>procedure Register;<br><br>implementation<br><br>type<br>&nbsp; TDirMonParams = record<br>&nbsp; &nbsp; &nbsp; WindowHandle : HWND;<br>&nbsp; &nbsp; &nbsp; hMutex &nbsp; &nbsp; &nbsp; : THandle;<br>&nbsp; &nbsp; &nbsp; Directory &nbsp; &nbsp;: array [0..255] of char;<br>&nbsp; &nbsp; &nbsp; NotifyFilter : DWORD;<br>&nbsp; end;<br>&nbsp; PDirMonParams = ^TDirMonParams;<br><br><br>{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}<br>procedure Register;<br>begin<br>&nbsp; &nbsp; RegisterComponents('FPiette', [TDirMon]);<br>end;<br><br><br>{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}<br>procedure TDirMon.SetDirectory(newValue : String);<br>begin<br>&nbsp; &nbsp; if newValue = FDirectory then<br>&nbsp; &nbsp; &nbsp; &nbsp; Exit;<br><br>&nbsp; &nbsp; if FThreadHandle &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; raise Exception.Create('Unable to change Directory ' +<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'when DirMon is running');<br><br>&nbsp; &nbsp; FDirectory := newValue;<br>end;<br><br><br>{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}<br>procedure TDirMon.SetNotifyFilter(newValue : TDirMonType);<br>begin<br>&nbsp; &nbsp; if FThreadHandle &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; raise Exception.Create('Unable to change NotifyFilter ' +<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'when DirMon is running');<br><br>&nbsp; &nbsp; FNotifyFilter := 0;<br>&nbsp; &nbsp; if dmcFileName in newValue then<br>&nbsp; &nbsp; &nbsp; &nbsp; FNotifyFilter := (FNotifyFilter or FILE_NOTIFY_CHANGE_FILE_NAME);<br>&nbsp; &nbsp; if dmcDirName in newValue then<br>&nbsp; &nbsp; &nbsp; &nbsp; FNotifyFilter := (FNotifyFilter or FILE_NOTIFY_CHANGE_DIR_NAME);<br>&nbsp; &nbsp; if dmcAttributes in newValue then<br>&nbsp; &nbsp; &nbsp; &nbsp; FNotifyFilter := (FNotifyFilter or FILE_NOTIFY_CHANGE_ATTRIBUTES);<br>&nbsp; &nbsp; if dmcSize in newValue then<br>&nbsp; &nbsp; &nbsp; &nbsp; FNotifyFilter := (FNotifyFilter or FILE_NOTIFY_CHANGE_SIZE);<br>&nbsp; &nbsp; if dmcLastWrite in newValue then<br>&nbsp; &nbsp; &nbsp; &nbsp; FNotifyFilter := (FNotifyFilter or FILE_NOTIFY_CHANGE_LAST_WRITE);<br>&nbsp; &nbsp; if dmcSecurity in newValue then<br>&nbsp; &nbsp; &nbsp; &nbsp; FNotifyFilter := (FNotifyFilter or FILE_NOTIFY_CHANGE_SECURITY);<br>end;<br><br><br>{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}<br>function TDirMon.GetNotifyFilter : TDirMonType;<br>begin<br>&nbsp; &nbsp; Result := [];<br>&nbsp; &nbsp; if (FNotifyFilter and FILE_NOTIFY_CHANGE_FILE_NAME) &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; Result := Result + [dmcFileName];<br>&nbsp; &nbsp; if (FNotifyFilter and FILE_NOTIFY_CHANGE_DIR_NAME) &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; Result := Result + [dmcDirName];<br>&nbsp; &nbsp; if (FNotifyFilter and FILE_NOTIFY_CHANGE_ATTRIBUTES) &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; Result := Result + [dmcAttributes];<br>&nbsp; &nbsp; if (FNotifyFilter and FILE_NOTIFY_CHANGE_SIZE) &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; Result := Result + [dmcSize];<br>&nbsp; &nbsp; if (FNotifyFilter and FILE_NOTIFY_CHANGE_LAST_WRITE) &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; Result := Result + [dmcLastWrite];<br>&nbsp; &nbsp; if (FNotifyFilter and FILE_NOTIFY_CHANGE_SECURITY) &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; Result := Result + [dmcSecurity];<br>end;<br><br><br>{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}<br>procedure TDirMon.TriggerDirChangeEvent;<br>begin<br>&nbsp; &nbsp; if assigned(FOnDirChange) then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FOnDirChange(Self);<br>&nbsp; &nbsp; &nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>end;<br><br><br>{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}<br>procedure TDirMon.TriggerStartEvent;<br>begin<br>&nbsp; &nbsp; if assigned(FOnStart) then<br>&nbsp; &nbsp; &nbsp; &nbsp; FOnStart(Self);<br>end;<br><br><br>{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}<br>procedure TDirMon.TriggerStopEvent;<br>begin<br>&nbsp; &nbsp; if assigned(FOnStop) then<br>&nbsp; &nbsp; &nbsp; &nbsp; FOnStop(Self);<br>end;<br><br><br>{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}<br>constructor TDirMon.Create(AOwner : TComponent);<br>begin<br>&nbsp; &nbsp; inherited Create(AOwner);<br>&nbsp; &nbsp; FWindowHandle := AllocateHWnd(WndProc);<br>&nbsp; &nbsp; FNotifyFilter := FILE_NOTIFY_CHANGE_FILE_NAME;<br>end;<br><br><br>{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}<br>destructor TDirMon.Destroy;<br>begin<br>&nbsp; &nbsp; if FThreadHandle &lt;&gt; 0 then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; ReleaseMutex(FMutexHandle);<br>&nbsp; &nbsp; &nbsp; &nbsp; WaitForSingleObject(FThreadHandle, INFINITE);<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; if FParamPtr &lt;&gt; nil then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; FreeMem(FParamPtr);<br>&nbsp; &nbsp; &nbsp; &nbsp; FParamPtr := nil;<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; if FMutexHandle &lt;&gt; 0 then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; CloseHandle(FMutexHandle);<br>&nbsp; &nbsp; &nbsp; &nbsp; FMutexHandle := 0;<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; DeallocateHWnd(FWindowHandle);<br>&nbsp; &nbsp; inherited Destroy;<br>end;<br><br><br>{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}<br>procedure TDirMon.WndProc(var MsgRec: TMessage);<br>begin<br>&nbsp; &nbsp; &nbsp;with MsgRec do begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if Msg = WM_DIRCHANGE then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WMDirChange(MsgRec)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := DefWindowProc(Handle, Msg, wParam, lParam);<br>&nbsp; &nbsp; end;<br>end;<br><br><br>{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}<br>procedure TDirMon.WMDirChange(var aMsg: TMessage);<br>begin<br>&nbsp; &nbsp; TriggerDirChangeEvent;<br>end;<br><br><br>{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}<br>function MonitorThread(Prm : Pointer) : DWORD; stdcall;<br>var<br>&nbsp; &nbsp; Hdl &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: THandle;<br>&nbsp; &nbsp; Status &nbsp; &nbsp; &nbsp; : DWORD;<br>&nbsp; &nbsp; pParams &nbsp; &nbsp; &nbsp;: PDirMonParams;<br>&nbsp; &nbsp; hObjects &nbsp; &nbsp; : array [0..1] of THandle;<br>begin<br>&nbsp; &nbsp; if Prm = nil then<br>&nbsp; &nbsp; &nbsp; &nbsp; ExitThread(1);<br><br>&nbsp; &nbsp; pParams := PDirMonParams(Prm);<br><br>&nbsp; &nbsp; Hdl := FindFirstChangeNotification(pParams^.Directory,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FALSE,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pParams^.NotifyFilter);<br>&nbsp; &nbsp; if Hdl = INVALID_HANDLE_VALUE then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Application.MessageBox('FindFirstChangeNotification() failed',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Warning', mb_OK);<br>&nbsp; &nbsp; &nbsp; &nbsp; ExitThread(GetLastError);<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; while (TRUE) do begin<br>&nbsp; &nbsp; &nbsp; &nbsp; hObjects[0] := Hdl;<br>&nbsp; &nbsp; &nbsp; &nbsp; hObjects[1] := pParams.hMutex;<br>&nbsp; &nbsp; &nbsp; &nbsp; Status &nbsp; &nbsp; &nbsp;:= WaitForMultipleObjects(2, @hObjects, FALSE, INFINITE);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; if Status = WAIT_OBJECT_0 then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PostMessage(pParams^.WindowHandle, WM_DIRCHANGE, 0, 0);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not FindNextChangeNotification(Hdl) then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FindCloseChangeNotification(Hdl);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ExitThread(GetLastError);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; if Status = (WAIT_OBJECT_0 + 1) then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FindCloseChangeNotification(Hdl);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ReleaseMutex(pParams.hMutex);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ExitThread(0);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>end;<br><br><br>{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}<br>procedure TDirMon.Start;<br>var<br>&nbsp; &nbsp; p : PDirMonParams;<br>begin<br>&nbsp; &nbsp; if FThreadHandle &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; raise Exception.Create('DirMon already started');<br><br>&nbsp; &nbsp; // Create mutex for stopping thread when needed<br>&nbsp; &nbsp; FMutexHandle := CreateMutex(nil, TRUE, nil);<br>&nbsp; &nbsp; if FMutexHandle = 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; raise Exception.Create('CreateMutex failed');<br><br>&nbsp; &nbsp; // Allocate some memory for thread parameters<br>&nbsp; &nbsp; GetMem(p, SizeOf(TDirMonParams));<br>&nbsp; &nbsp; FParamPtr &nbsp; &nbsp; &nbsp; := p;<br>&nbsp; &nbsp; p^.WindowHandle := FWindowHandle;<br>&nbsp; &nbsp; p^.hMutex &nbsp; &nbsp; &nbsp; := FMutexHandle;<br>&nbsp; &nbsp; p^.NotifyFilter := FNotifyFilter;<br>&nbsp; &nbsp; StrCopy(p^.Directory, PChar(FDirectory));<br><br>&nbsp; &nbsp; // Start the working thread<br>&nbsp; &nbsp; FThreadHandle := CreateThread(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// pointer to thread security attributes<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// initial thread stack size, in bytes<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@MonitorThread, &nbsp; &nbsp; &nbsp; &nbsp;// pointer to thread function<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FParamPtr, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // argument for new thread<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// creation flags<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FThreadId); &nbsp; &nbsp; &nbsp;// pointer to returned thread identifier<br><br>&nbsp; &nbsp; if FThreadHandle = 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; raise Exception.Create('CreateThread failed');<br><br>&nbsp; &nbsp; TriggerStartEvent;<br>end;<br><br><br>{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}<br>procedure TDirMon.Stop;<br>begin<br>&nbsp; &nbsp; if FThreadHandle = 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; raise Exception.Create('DirMon not started');<br><br>&nbsp; &nbsp; if FMutexHandle &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; ReleaseMutex(FMutexHandle);<br><br>&nbsp; &nbsp; if FThreadHandle &lt;&gt; 0 then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; WaitForSingleObject(FThreadHandle, INFINITE);<br>&nbsp; &nbsp; &nbsp; &nbsp; FThreadHandle := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; FThreadID &nbsp; &nbsp; := 0;<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; if FMutexHandle &lt;&gt; 0 then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; CloseHandle(FMutexHandle);<br>&nbsp; &nbsp; &nbsp; &nbsp; FMutexHandle := 0;<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; if FParamPtr &lt;&gt; nil then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; FreeMem(FParamPtr);<br>&nbsp; &nbsp; &nbsp; &nbsp; FParamPtr := nil;<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; TriggerStopEvent;<br>end;<br><br><br>{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}<br><br>end.<br><br>{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}<br>
 
使用这个目录监控控件即可.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
930
DelphiTeacher的专栏
D
后退
顶部