Search code examples
loopsfilesystemwatcher

How do I keep watching a directory until exit button is pressed?


Thanks in advance, easy one for most no doubt but I am new to programming.

My first app is a winform app that allows user to choose a directory and when they click on start, a FileSystemWatcher is initialised. All works and I have handled changes, deletions etc but my problem is that it only handles one event (albeit correctly) but after that it closes.

I need to set

Watcher.EnableRaisingEvents=True

until the exit button is pressed?

Here the code is currently:

public void btnStart(object sender, EventArgs e)
    {
        //create watcher and set properties

        FileSystemWatcher Watcher1 = new FileSystemWatcher();

        Watcher1.Path = txtBoxDirToWatch.Text;

        if (Watcher1.Path.Length <2)
        {
            MessageBox.Show("Path does not exist, Please reselect");
            return;
        }

        //user response to close
        MessageBox.Show("Your directory is now being monitored for changes");

        Watcher1.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;

        //watch all files in the path 

        Watcher1.Filter = "*.*";

        //add event handlers

        Watcher1.Changed += new FileSystemEventHandler(OnChanged);
        Watcher1.Created += new FileSystemEventHandler(OnChanged);
        Watcher1.Deleted += new FileSystemEventHandler(OnDeleted);
        Watcher1.Renamed += new RenamedEventHandler(OnRenamed);

        //begin watching

        Watcher1.EnableRaisingEvents = true;    

    }

    public static void OnChanged(object source, FileSystemEventArgs e)

    {

        //need something in here to deal with changes and copy to hidden directory

        string destination = Path.Combine(@"C:\Watcher\sync", e.Name);

        File.Copy(e.FullPath, destination);

   }

    public static void OnRenamed(object source, FileSystemEventArgs e)
    {

then I handle onrenamed, ondeleted etc

Can anyone give a shove in the right direction please?

Thanks


Solution

  • The solution here was to create a new thread for each on program start and set EnableRaisingEvents = new on each.

    new Thread(ReNamed).Start(); new Thread(Changed).Start(); new Thread(Deleted).Start(); new Thread(Created).Start();

    void ReNamed()
        {
            FileSystemWatcher Watcher2 = new FileSystemWatcher();
    
            Watcher2.Path = txtBxDirToWatch.Text;
            Watcher2.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName;// | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.DirectoryName;
    
            //watch all files in the path 
    
            Watcher2.Filter = "*.*";
    
            //dont watch subdirectories as default
            Watcher2.IncludeSubdirectories = false;
            if (chkBxIncSub.Checked)
            {
            Watcher2.IncludeSubdirectories = true;
            }
    
            Watcher2.Renamed += new RenamedEventHandler(OnRenamed);
            Watcher2.EnableRaisingEvents = true;
        }