How to monitor files in different folders using the FileSystemWatcher?

Hi

This is what i came up with but it doesn't monitor these folders

simultaneously


Answer this question

How to monitor files in different folders using the FileSystemWatcher?

  • 77elcomama

    Then you need to wire the Changed, Created, Deleted, Renamed and Error event.

    The Created event will be fired when a file is created for example.
    And don't reset the fileWatcher.Path = path; everytime. Because you overwrite the current path.

    Foreach root you must create an dedicated FileSystemWatcher object!


  • RajeshJV

    I only execute this code once the button is clicked.

  • Vargaj

    Did you wire the events

    And you resert the Filepath every time in the loop. For each root directory you need a dedicated FileSystemWatcher object.



  • B0tt

    Yes it is, here is a little self-explaining code i hope:


    private ArrayList _watchers = new ArrayList();

    protected void WatchRootDirectory( string root )
    {
    // Create filesystemwatcher and set property values.
    FileSystemWatcher watcher = new FileSystemWatcher( root );
    watcher.IncludeSubdirectories = true;
    watcher.EnableRaisingEvents = true;
    watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
    | NotifyFilters.FileName | NotifyFilters.DirectoryName;

    // Wire events.
    watcher.Changed += new FileSystemEventHandler(watcher_Changed);
    watcher.Created += new FileSystemEventHandler(watcher_Created);
    watcher.Deleted += new FileSystemEventHandler(watcher_Deleted);

    // Add to collection.
    _watchers.Add( watcher );
    }

    private void watcher_Changed(object sender, FileSystemEventArgs e)
    {
    MessageBox.Show( this, e.FullPath + " changed." );
    }

    private void watcher_Created(object sender, FileSystemEventArgs e)
    {
    MessageBox.Show( this, e.FullPath + " created." );
    }

    private void watcher_Deleted(object sender, FileSystemEventArgs e)
    {
    MessageBox.Show( this, e.FullPath + " deleted." );
    }




  • BeyondTheSunset

    Does anyone happen to have the VB 2005 version of this

    I tried to figure it out but I've got the sqiggleez happening everywhere.


  • CplusplusBeginner

    P.J. I couldn't thank you more



  • Chadh

  • Welles

    Is it possible to add the File System Watcher at runtime because I want to monitor more than 16 Different folders in different locations

  • How to monitor files in different folders using the FileSystemWatcher?