The Code Project View our sponsorsClick here for Whole Tomato Software - Home of Visual AssistAdvertise on the CodeProject
Home >> Files & Folders >> General

CDirectoryChangeWatcher -- ReadDirectoryChangesW all wrapped up
By Wes Jones

This class wraps up ReadDirectoryChangesW. 
 VC6, NT4, W2K, MFC
 Posted 1 Feb 2001
 Updated 8 May 2001
Articles by this author
Send to a friend
Printer friendly version
Lounge New Articles Sign in Forums Contribute
Broken links? Email us!
23 users have rated this article. result:
4.87 out of 5.

 [Sample Image - 50K]

Introduction

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.

The Classes

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.

[Top] Sign in to vote for this article:     PoorExcellent  
Hint: For improved responsiveness, use Internet Explorer 4 (or above) with Javascript enabled, choose 'Use DHTML' from the View dropdown and hit 'Set Options'.
 Keyword Filter
 View   Per page   Messages since
New threadMessages 1 to 12 of 12 (Total: 12)First Prev Next Last
Subject 
Author 
Date 
  DLL for VB
VLE 12:10 30 May 01 
  Re: DLL for VB - reply from author
Wes Jones 13:20 30 May 01 
  What about win 95/98?
philip andrew 22:55 27 Mar 01 
  Re: What about win 95/98?
Sardaukar 5:30 1 May 01 
  Re: What about win 95/98?
Chris Ternoey 10:58 3 May 01 
  Re: What about win 95/98?
Chris Ternoey 11:02 3 May 01 
  Re: What about win 95/98?
George Chastain 11:13 8 May 01 
  Non Windows Drives
Don Sanders 16:02 22 Mar 01 
  Re: Non Windows Drives
Wes Jones 17:54 22 Mar 01 
  Re: Non Windows Drives
Don Sanders 22:07 26 Mar 01 
  SeBackupPrivilege
Don Sanders 15:21 22 Mar 01 
  Re: SeBackupPrivilege - reply from author
Wes Jones 17:17 22 Mar 01 
Last Visit: 12:00 Friday 1st January, 1999First Prev Next Last

Home >> Files & Folders >> General
last updated 8 May 2001
Article content copyright Wes Jones, 2001
everything else © CodeProject, 1999-2001.
The Code Project View our sponsorsGet your new domain name now!Advertise on the CodeProject