
Introduction
Sometimes, its is very important to monitor events, occurring in some directory. For example, if new file was saved in a specified directory, the application must to do something.
Background
File system notification events can be obtained by the Win32 API function FindFirstChangeNotification
(look more in detail in the MSDN). This function creates a change notification handle and sets up initial change notification filter conditions. A wait on a notification handle succeeds when a change matching the filter conditions occurs in the specified directory or subtree. After the wait has been satisfied, the application can respond to this condition and continue monitoring the directory, by calling the FindNextChangeNotification
function and the appropriate wait function. When the handle is no longer needed, it can be closed by using the FindCloseChangeNotification
function.
The classes
CNotifyDirCheck
class holds notification events in the work thread and parse file tree for finding a new (changed) file. User must override the Action
function of the base class or implement own notification callback. This callback has the following prototype:
UINT DefaultNotificationCallback(
CFileInformation fiObject,
EFileAction faAction,
LPVOID lpData );
If the callback succeeds, the return value is 0. If the callback failed, the return value must be an error number (>0). Samples of the callback and virtual function Action
exists in the code of the class CNotifyDirCheck
.
Class CFileInformation
is a useful API wrapping class that is developed to make file manipulation and to obtain information about files and directories in a simpler and easier way.
How to Use
- Create an object of
CNotifyDirCheck
.
- Create notification callback or override virtual function
Action
.
- Select directory.
- Add the user�s data pointer (if needed).
- Call method
Run
to start notification thread.
- Call method
Stop
to stop notification thread (or this method will be called in destructor).
CNotifyDirCheck m_ndc;
�
UINT DirCallback(...) {...}
...
m_ndc.SetDirectory( m_dir );
m_ndc.SetData( this );
m_ndc.SetActionCallback( DirCallback );
�
m_ndc.Run();
...
m_ndc.Stop();
Conclusion
I would like to mention thanks to CodeProject authors for many great ideas and useful source code. I'd be glad if somebody uses my class. Please let me know of any bugs that you have found and I will fix them in a future release.
References
- MSDN
- CodeProject - Files & Folders