Obtaining Directory change Notifications (Microsoft example, using Findfirstchangenotification,findnextchangenotification, Findclosechangenotification API function)

Source: Internet
Author: User

The application can monitor the contents of a directory and its subdirectories by using the change notifications. Waiting for a change notification was similar to have 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 Monitore D directory changes.

An application can specify a set of conditions, trigger a change notification by using the Findfirstchangenotifica tion 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 the can is waited on by using the wait functions. If The wait condition is satisfied, findnextchangenotification can being used to provide a notification handle to WA It on subsequent changes. However, these functions do not indicate the actual of change that satisfied the wait condition.

Use findclosechangenotification to close the notification handle.

To retrieve information on the specific change as part of the notification, use the readdirectorychangesw func tion. This function also enables provide a completion routine.

To track changes in 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 thefindfirstchangenotification function to create the notification handles and the Waitformu Ltipleobjects 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.

 C++ 
#include <windows.h> #include <stdlib.h> #include <stdio.h> #include <tchar.h>void    Refreshdirectory (LPTSTR), void Refreshtree (LPTSTR), void Watchdirectory (LPTSTR), void _tmain (int argc, TCHAR *argv[]) {        if (argc! = 2) {_tprintf (TEXT ("Usage:%s <dir>\n"), argv[0]);    Return } watchdirectory (Argv[1]);}    void Watchdirectory (LPTSTR lpdir) {DWORD dwwaitstatus;    HANDLE dwchangehandles[2];   TCHAR Lpdrive[4];   TCHAR Lpfile[_max_fname];   TCHAR Lpext[_max_ext];   _tsplitpath_s (Lpdir, Lpdrive, 4, NULL, 0, Lpfile, _max_fname, Lpext, _max_ext);   LPDRIVE[2] = (TCHAR) ' \ \ '; LPDRIVE[3] = (TCHAR) ' + ';     Watch the directory for file creation and deletion.                         Dwchangehandles[0] = findfirstchangenotification (lpdir,//directory to watch FALSE, Do not watch subtree file_notify_change_file_name);   Watch file name changes if (dwchangehandles[0] = = Invalid_handle_value) {  printf ("\ n error:findfirstchangenotification function failed.\n");    ExitProcess (GetLastError ());     }//Watch the subtree for directory creation and deletion.                          DWCHANGEHANDLES[1] = findfirstchangenotification (lpdrive,//directory to watch TRUE,  Watch the subtree file_notify_change_dir_name); Watch dir name changes if (dwchangehandles[1] = = Invalid_handle_value) {printf ("\ n Error:findfirstchangeno     tification function failed.\n ");    ExitProcess (GetLastError ());   }//Make a final validation check in our handles. if ((dwchangehandles[0] = = NULL) | |   (Dwchangehandles[1] = = NULL))     {printf ("\ n error:unexpected NULL from findfirstchangenotification.\n");    ExitProcess (GetLastError ()); }//change notification is set.     Now wait on both notification//handles and refresh accordingly.       while (TRUE) {//Wait for notification.  printf ("\nwaiting for notification...\n");    Dwwaitstatus = WaitForMultipleObjects (2, Dwchangehandles, FALSE, INFINITE); Switch (dwwaitstatus) {case WAIT_OBJECT_0://A file is created, renamed, or deleted in the Dir         Ectory.              Refresh This directory and restart the notification.              Refreshdirectory (Lpdir); if (Findnextchangenotification (dwchangehandles[0]) = = FALSE) {printf ("\ n Error:findnextchang               enotification function failed.\n ");              ExitProcess (GetLastError ());           } break;         Case WAIT_OBJECT_0 + 1://A directory is created, renamed, or deleted.              Refresh the tree and restart the notification.              Refreshtree (lpdrive); if (Findnextchangenotification (dwchangehandles[1]) = = FALSE) {printf ("\ n error:findnextchange               Notification function failed.\n ");              ExitProcess (GetLastError ());           }  Break Case Wait_timeout://A-TIMEOUT occurred, this would happen if some value other//than INFINITE is used         In the Wait call and no changes occur.             In a single-threaded environment your might not want a//INFINITE wait.            printf ("\nno changes in the timeout period.\n");         Break            default:printf ("\ n error:unhandled dwwaitstatus.\n");            ExitProcess (GetLastError ());      Break  }}}void refreshdirectory (LPTSTR lpdir) {//This was where you might place code to refresh your//directory listing,   But not the subtree because it//would isn't be necessary. _tprintf (TEXT ("Directory (%s) changed.\n"), Lpdir);} void Refreshtree (LPTSTR lpdrive) {//This was where you might place code to refresh your//directory listing, Includin   G The subtree. _tprintf (TEXT ("Directory Tree (%s) changed.\n"), lpdrive);}

https://msdn.microsoft.com/en-us/library/windows/desktop/aa365261 (v=vs.85). aspx

Obtaining Directory change Notifications (Microsoft example, using Findfirstchangenotification,findnextchangenotification, Findclosechangenotification API function)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.