Monitor events in a specific folder: Three Solutions

Source: Internet
Author: User

In the ldbivr program, you need to monitor the file landing or deletion events in a specific folder. The event notification mechanism in shell is used. Later, you need to consider various unexpected events, such as unexpected service exit and server crash, then let it go.
There are roughly three methods for asynchronous notification of directory events:
Call the Windows API function readdirectorychanges to monitor files in a specified directory (recommended). asynchronous Io is used:
Http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/fileio/base/readdirectorychangesw. asp
The readdirectorychangesw function retrieves information that describes the changes within a directory.
To track changes on a volume, see change journals.
Bool readdirectorychangesw (
Handle hdirectory,
Lpvoid lpbuffer,
DWORD nbufferlength,
Bool bwatchsubtree,
DWORD dwpolicyfilter,
Lpdword lpbytesreturned,
Lpoverlapped,
Lpoverlapped_completion_routine lpcompletionroutine
);
  
You can also use shchangenotifyregister and shchangenotifyderegister to register the event shchangenotify:
Http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/shellcc/platform/Shell/reference/functions/shchangenoworkflow. asp
  
Shchangenoworkflow Function
  
Notifies the system of an event that an application has generated med. An application shocould use this function if it performs an action that may affect the shell.
Syntax
Void shchangenoworkflow (
Long weventid,
Uint uflags,
Lpcvoid dwitem1,
Lpcvoid dwitem2
);
This shell event, Someone introduced it like this (http://qk.5nx.com/article_view.asp? Id = 11 ).
You can also call the API functions-findfirstchangenotification, findclosechangenotification, findnextchangenotification, and waitforsingleobject:
Http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/fileio/base/findfirstchangenotification. asp
Findfirstchangenotification
The findfirstchangenotification 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. however, the function does not indicate the change that satisfied the wait condition.
To retrieve information about the specific change as part of the notification, use the readdirectorychangesw function.
Handle findfirstchangenotification (
Lpctstr lppathname,
Bool bwatchsubtree,
DWORD dwpolicyfilter
);
The famous Filemon tool uses virtual drivers.
Related Resources:
1: Monitoring Program compilation;
2: Directory monitoring in Win32;
3: Change journals;
4: cdirectorychangewatcher-readdirectorychangesw all wrapped up;
5: Monitoring directory activities;
6: How can I monitor system access to files?

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.