Click here to Skip to main content
Licence 
First Posted 3 Aug 2003
Views 83,108
Bookmarked 41 times

How to get a notification if change occurs in a specified directory

By | 3 Aug 2003 | Article
This article discusses how to handle file system notification events.

Sample Image - DirCheck.gif

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,  //file (directory) information object
EFileAction       faAction,  //notification event type
LPVOID           lpData );  //user's data pointer

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

  1. Create an object of CNotifyDirCheck.
  2. Create notification callback or override virtual function Action.
  3. Select directory.
  4. Add the user’s data pointer (if needed).
  5. Call method Run to start notification thread.
  6. 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 );//root directory
m_ndc.SetData( this );//user’s data
m_ndc.SetActionCallback( DirCallback );//user’s callback
…
m_ndc.Run();//start workthread
...
m_ndc.Stop();//stop workthread

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

  1. MSDN
  2. CodeProject - Files & Folders

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

jukov

Web Developer

Israel Israel

Member

Software developer since 1992

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMultiple Notifications PinmemberDoGuy3:31 10 Oct '05  
GeneralRe: Multiple Notifications Nice Job Pinmemberbruno leclerc23:58 2 Feb '06  
GeneralRe: Multiple Notifications Nice Job Pinmemberacjohnson5512:17 17 Jul '06  
GeneralMultiple notifications PinmemberMadhu Reddy1:23 9 Mar '05  
Generalsome bug... Pinmemberboy061220:35 12 May '04  
GeneralGreat help! Pinmembermr.axe8:39 6 Oct '03  
GeneralHello Monster! PinsussLaguna Men8:53 13 Aug '03  
GeneralRe: Hello Monster! Pinmemberjukov21:01 23 Aug '03  
QuestionIs this better? PinsussAnonymous7:35 4 Aug '03  
AnswerRe: Is this better? Pinmemberdwccgc10:14 5 Aug '03  
GeneralRe: Is this better? Pinmemberzhulei_cug17:15 13 Oct '05  
GeneralERROR N50 Pinmemberdwccgc6:54 4 Aug '03  
GeneralRe: ERROR N50 Pinmemberjukov21:49 6 Aug '03  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 4 Aug 2003
Article Copyright 2003 by jukov
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid