Beispiel FileSystemWatcher



  • Gibts irgendwo ein Beispiel für Managed C++, das den Watcher beinhaltet? Einfaches Konsolenprogramm genügt...



  • Nimm doch das beispiel aus C++/CLI:
    http://msdn2.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
    und ersetze ^ durch * und gcnew durch new...



  • Danke, funktioniert. An die msdn2 hatte ich nicht gedacht.
    Mein Code sieht so aus:

    #using <mscorlib.dll>
    
    using namespace System;
    using namespace System::IO;
    
    public __gc 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, __box(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 );
       }
    
    public:
       void run() {
    
         // Create a new FileSystemWatcher and set its properties.
         FileSystemWatcher* watcher = new FileSystemWatcher;
         watcher->Path = AppDomain::CurrentDomain->BaseDirectory;
    
         /* Watch for changes in LastAccess and LastWrite times, and 
             the renaming of files or directories. */
         watcher->NotifyFilter = static_cast<NotifyFilters>(NotifyFilters::LastAccess |
                NotifyFilters::LastWrite | NotifyFilters::FileName | NotifyFilters::DirectoryName);
    
         // Only watch text files.
         watcher->Filter = "*.txt";
    
         // Add event handlers.
         // hier wurde this-Zeiger eingefügt
         watcher->Changed += new FileSystemEventHandler(this, Watcher::OnChanged );
         watcher->Created += new FileSystemEventHandler(this, Watcher::OnChanged );
         watcher->Deleted += new FileSystemEventHandler(this, Watcher::OnChanged );
         watcher->Renamed += new RenamedEventHandler(this, Watcher::OnRenamed );
    
         // Begin watching.
         watcher->EnableRaisingEvents = true;
    
         // Wait for the user to quit the program.
         Console::WriteLine( "Press \'q\' to quit the sample." );
         while ( Console::Read() != 'q' )
           ;
       }
    };
    
    int _tmain() {
    
      // Watcher::run(); // von msdn2
      Watcher* w = new Watcher();
      w->run();
    
      return 0;
    }
    

    Noch eine Frage:

    (edit) geklärt...


Anmelden zum Antworten