FileSystemWatcher of file Monitoring (C + +)

Source: Internet
Author: User

In order to monitor the static file of the Web program is malicious change, so learned the FileSystemWatcher class to the file monitoring, because still in the initial stage, here only a few about FileSystemWatcher learning code.

The specific code is as follows:

#using<System.dll>#include<iostream>using namespacestd;using namespaceSystem;using namespaceSystem::IO;using namespacesystem::security::P ermissions; Public ref classwatcher{Private:   //Define the event handlers.   Static voidOnChanged (object^/*Source*/, filesystemeventargs^e) {//Specify what was done when a file was changed, created, or deleted.Console::WriteLine ("File: {0} {1}", E->fullpath, e->changetype); }   Static voidOnrenamed (object^/*Source*/, renamedeventargs^e) {//Specify what was done when a file was renamed.Console::WriteLine ("File: {0} renamed to {1}", E->oldfullpath, e->FullPath); } Public: [PermissionSet (SecurityAction::D emand, Name="FullTrust")]   int Staticrun () {//Array<string^>^args = System::environment::getcommandlineargs (); //creates a FileSystemWatcher and sets its properties.filesystemwatcher^ Fswatcher =gcnew FileSystemWatcher (); Fswatcher->path ="C:\\files"; /*Watch for changes in Lastaccess and lastwrite times, and the renaming of files or directories.*/Fswatcher->notifyfilter = static_cast<notifyfilters> (//the following properties of the listener file are added as required here I have added some commonnotifyfilters::lastaccess |//the date on which the file or folder was last opened. Notifyfilters::lastwrite |//The last date the content was written to a file or folderNotifyfilters::filename |//file nameNotifyfilters::D irectoryname |//Directory nameNotifyfilters::size);//size//Listen to subdirectoriesFswatcher->includesubdirectories =true; //Only watch text files. //fswatcher->filter = "*.txt"; //ADD event handlers.Fswatcher->changed + =gcnew FileSystemEventHandler (watcher::onchanged); Fswatcher->created + =gcnew FileSystemEventHandler (watcher::onchanged); Fswatcher->deleted + =gcnew FileSystemEventHandler (watcher::onchanged); Fswatcher->renamed + =gcnew Renamedeventhandler (watcher::onrenamed); //Begin watching.Fswatcher->enableraisingevents =true; //Wait for the user to quit the program.Console::WriteLine ("Press \ ' q\ ' to quit the sample." );  while(Console::read ()! ='Q' ); return 0; }};intMain () {Watcher::run ();}

Procedure 1. First create a FileSystemWatcher object to set some properties and add listener events

2. Setting the Listening directory

3. Setting the properties of a listening file

4. Setting up a listener subdirectory

5. Adding a listener Event

6. Start monitoring

The sample code above can be found on MSDN, and if you have an indeterminate place, you can view the document

FileSystemWatcher of file Monitoring (C + +)

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.