Tuesday, December 15, 2009

4.7 Detecting Changes to Directories and Files



< BACK  NEXT >

[oR]


4.7
Detecting Changes to Directories and Files


The 32-bit APIs contains a set of functions that allow you to detect changes in files and directories. For example, if you set up a change notification handle on a certain directory and a new file appears in that directory, you can detect it in your application.



Two applications that allow you to see this capability in action are the File Manager and Visual C++ (VC++). In VC++, if someone modifies a file that VC++ has open, then VC++ will recognize the change and advise you of it. For example, another user might attach to your drive over the network, open a file with Notepad, modify it, and write it back to the disk. VC++ will post an error dialog when it detects the change, and ask you what to do. In the File Manager, if the contents of the drive change after a search operation and the search window is still visible, then the File Manager will ask if you want to re-search the drive to update the window.



To use the change-notification functions, you must indicate a directory for the functions to watch, specify whether you want them to watch only the directory or the directory plus all of its sub-trees, and what you want them to watch for. The change notification functions can detect any or all of the following changes to directories:





  • FILE_NOTIFY_CHANGE_FILE_NAME: The functions notify you when any file name change occurs due to renaming, creation, or deletion.





  • FILE_NOTIFY_CHANGE_DIR_NAME: The functions notify you when any directory name change occurs due to renaming, creation, or deletion.





  • FILE_NOTIFY_CHANGE_ATTRIBUTES: The functions notify you when any file attribute changes.





  • FILE_NOTIFY_CHANGE_SIZE: The functions notify you when any file changes size due to a file write operation.





  • FILE_NOTIFY_CHANGE_LAST_WRITE: The functions notify you when any file's last-write time changes





  • FILE_NOTIFY_CHANGE_SECURITY: The functions notify you when any file's security descriptor changes.





Section 7.6 contains an example and shows how to wait for the change notification in both a standard and an MFC program.





< BACK  NEXT >


No comments: