|
Broken links? Email us! |
|
This code wraps up the Win32 API function ReadDirectoryChangesW
so that your application only has
to worry about responding to the events that take place when a file or directory is added, removed, modified, or
renamed.
This code will only work on Windows NT or Windows 2000, and the directory you wish to watch must also reside on a Windows NT or Windows 2000 computer.
There are two classes that must be used together to watch a directory, they are: CDirectoryChangeWatcher
and CDirectoryChangeHandler
.
The public interface of CDirectoryChangeWatcher
:
class CDirectoryChangeWatcher{ public: CDirectoryChangeWatcher();//ctor virtual ~CDirectoryChangeWatcher();//dtor DWORD WatchDirectory(const CString & strDirToWatch, DWORD dwChangesToWatchFor, CDirectoryChangeHandler * pChangeHandler, BOOL bWatchSubDirs = FALSE); BOOL IsWatchingDirectory(const CString & strDirName); BOOL UnwatchDirectory(const CString & strDirToStopWatching); BOOL UnwatchAllDirectories(); ... };
The class CDirectoryChangeHandler
has the following interface:
class CDirectoryChangeHandler{ public: CDirectoryChangeHandler(); //ctor virtual ~CDirectoryChangeHandler(); //dtor ... BOOL UnwatchDirectory(); CString GetChangedDirectoryName(); protected: //override these functions: virtual void On_FileAdded(const CString & strFileName); virtual void On_FileRemoved(const CString & strFileName); virtual void On_FileModified(const CString & strFileName); virtual void On_FileNameChanged(const CString & strOldFileName, const CString & strNewFileName); virtual void On_ReadDirectoryChangesError(DWORD dwError); ... };
To handle the events that happen when a file or directory is added, deleted, modified, or renamed, create a
class derived from CDirectoryChangeHandler
that does all of the things that you want to do when these
events happen.
class CMyDirectoryChangeHandler : public CDirectoryChangeHandler{...};
To watch a directory call CDirectoryChangeWatcher::WatchDirectory()
. For example:
m_DirectoryWatcher.WatchDirectory(_T("C:\\Temp"), FILE_CHANGE_NOTIFY_CREATION /* | other flags*/, &m_MyChangeHandler);
As files are changed, renamed, etc., the appropriate CDirectoryChangeHandler::On_Filexxx()
function
will be called.
A single instance of CDirectoryChangeWatcher
can be used to watch multiple directories at the same
time. It is also possible to use a different CDirectoryChangeHandler
for each watched directory, or
to use the same CDirectoryChangeHandler
for any number of watched directories.
CDirectoryChangeWatcher
was based on the FWatch example program in the SDK and uses an I/O completion
port so that there will only be one worker thread (per instance of CDirectoryChangeWatcher
) for any
number of watched directories.
These classes were designed to be thread-safe. Please note that the CDirectoryChangeHandler::On_Filexxxxx()
functions will be called in the context of a worker thread in your program.
Download the sample program or source code for more details.
Special thanks to Steve Harvey for helping me fix some bugs.
Feel free to email me with bugs, bug fixes, tips, comments, accolades, or admonitions at wesj@hotmail.com.
|
|