File Monitoring of FileSystemWatcher (c ++)

File Monitoring of FileSystemWatcher (c ++)

  In order to monitor whether the static files of the web program have been maliciously modified, I learned about the FileSystemWatcher class to monitor the files. Since it is still in its infancy, only some code about FileSystemWatcher learning is posted here.

  The specific code is as follows:

Copy code

#using <System.dll>
#include <iostream>

using namespace std;
using namespace System;
using namespace System::IO;
using namespace System::Security::Permissions;

public ref class Watcher
{
private:
   // Define the event handlers.
   static void OnChanged( Object^ /*source*/, FileSystemEventArgs^ e )
   {
      // Specify what is done when a file is changed, created, or deleted.
      Console::WriteLine( "File: {0} {1}", e->FullPath, e->ChangeType );
   }

   static void OnRenamed( Object^ /*source*/, RenamedEventArgs^ e )
   {
      // Specify what is done when a file is renamed.
      Console :: WriteLine ("File: {0} renamed to {1}", e-> OldFullPath, e-> FullPath); 
      / * Watch for changes in LastAccess and LastWrite times, and 
          the renaming of files or directories. * / 
      fsWatcher-> NotifyFilter = static_cast <NotifyFilters> (// The following attributes of the monitoring file are added as required. Here I add Some commonly used
   } 

public: 
   [PermissionSet (SecurityAction :: Demand, Name = "FullTrust")] 
   int static run () 
   { 
      // array <String ^> ^ args = System :: Environment :: GetCommandLineArgs (); 
      // Create a FileSystemWatcher and Set its properties. 
      FileSystemWatcher ^ fsWatcher = gcnew FileSystemWatcher (); 
      fsWatcher-> Path = "C: \\ files"; 

                                NotifyFilters :: LastAccess | // The date the file or folder was last opened. 
                                NotifyFilters :: LastWrite | // The date the content was last written to the file or folder 
                                NotifyFilters :: FileName | // File name 
                                NotifyFilters :: DirectoryName | // Directory name 
                                NotifyFilters :: Size); // Size
 
      // listener Directory 
      fsWatcher-> 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;
   }
};

int main() {
   Watcher::run();
}

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

   2. Set the listening directory

   3. Set the properties of the listening file

   4. Set the monitoring subdirectory

   5. Add monitoring events

   6. Start listening

The sample code above can be found on MSDN, if there is any uncertainty, you can view the documentation

Published 17 original articles · Like 228 · Visit 330,000+

Guess you like

Origin blog.csdn.net/cxu123321/article/details/105470831