Obtaining Directory Change Notifications
An application can monitor the contents of a directory and its subdirectories by using change notifications. Waiting for a change notification is similar to having a read operation pending against a directory and, if necessary, its subdirectories. When something changes within the directory being watched, the read operation is completed. For example, an application can use these functions to update a directory listing whenever a file name within the monitored directory changes.
An application can specify a set of conditions that trigger a change notification by using the FindFirstChangeNotification function. The conditions include changes to file names, directory names, attributes, file size, time of last write, and security. This function also returns a handle that can be waited on by using the wait functions. If the wait condition is satisfied, FindNextChangeNotification can be used to provide a notification handle to wait on subsequent changes. However, these functions do not indicate the actual change that satisfied the wait condition.
Use FindCloseChangeNotification to close the notification handle.
To retrieve information about the specific change as part of the notification, use the ReadDirectoryChangesW function. This function also enables you to provide a completion routine.
To track changes on a volume, see change journals.
The following example monitors the directory tree for directory name changes. It also monitors a directory for file name changes. The example uses the FindFirstChangeNotification function to create two notification handles and the WaitForMultipleObjects function to wait on the handles. Whenever a directory is created or deleted in the tree, the example should update the entire directory tree. Whenever a file is created or deleted in the directory, the example should refresh the directory.
Note
This simplistic example uses the ExitProcess function for termination and cleanup, but more complex applications should always use proper resource management such as FindCloseChangeNotification where appropriate.
#include <windows.h> void RefreshDirectory(LPTSTR); void _tmain(int argc, TCHAR *argv[]) WatchDirectory(argv[1]); void WatchDirectory(LPTSTR lpDir) _tsplitpath_s(lpDir, lpDrive, 4, NULL, 0, lpFile, _MAX_FNAME, lpExt, _MAX_EXT); lpDrive[2] = (TCHAR)'\\'; // Make a final validation check on our handles. // Change notification is set. Now wait on both notification dwWaitStatus = WaitForMultipleObjects(2, dwChangeHandles, // A time-out occurred. This would happen if some value other default: void RefreshDirectory(LPTSTR lpDir) _tprintf(TEXT("Directory (%s) changed.\n"), lpDir); void RefreshTree(LPTSTR lpDrive) _tprintf(TEXT("Directory tree (%s) changed.\n"), lpDrive); |
From :
MSDN (http://msdn.microsoft.com/en-us/library/aa365261(VS.85).aspx)
Another Reference Sites :
CFileChangeEvent Class (http://www.codeproject.com/KB/files/filechange.aspx)
How to get a notification if change occurs in a specified directory (http://www.codeproject.com/KB/files/DirCheck.aspx)