Search code examples
c#filesystemwatcher

How to count number of changes on multiple locations?


I'm using FileSystemWatcher to monitor multiple folders on a network storage. The problem is that I have to count the number of changes for each location. The counter is not correctly incremented because when the file from the second location is changed the counter is already set to 2.

I have the following code:

private void button1_Click(object sender, EventArgs e)
{
    FileMonitor(Directory.GetCurrentDirectory());
    FileMonitor(@"C:\Users\NET\Desktop");
}

static int _counter = 1;
public static int Counter
{
    get { return _counter; }
    set { _counter = value; }
}

private void FileMonitor(string path)
{
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = path;
    watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
   | NotifyFilters.FileName | NotifyFilters.DirectoryName;
    watcher.Filter = "*.csv";
    watcher.Changed += (s, e) => OnChanged(e.FullPath, watcher);

    watcher.EnableRaisingEvents = true;
}

private static void OnChanged(string path, FileSystemWatcher watcher)
{
    try
    {
        watcher.EnableRaisingEvents = false;
        MessageBox.Show(Counter.ToString());
        Counter = Counter + 1;
    }
    finally
    {
        watcher.EnableRaisingEvents = true;
    }
}

How can I set the counter to be unique for each FileMonitor instance?


Solution

  • The problem is that you make your counter static, so this counter is shared for all monitors. There are some ways that can help you:

    1. You can change your counter, for a list of counters. Then your first monitor uses counters[0] and so on.
    2. You can create a class with two properties, the monitor and the counter, and every time that you enter in OnChanged you can increase the counter of this monitor.

    I hope this can help you.

    Edit:
    MainWindow.xaml.xs

     List<FileMonitor> monitors = new List<FileMonitor>();
    
            public MainWindow()
            {
                InitializeComponent();
            } 
            private void Button_Click(object sender, RoutedEventArgs e)
            {
              monitors.Add(new FileMonitor(@"C:"));
              monitors.Add(new FileMonitor(@"D:"));
            }
    

    FileMonitor.cs

    public class FileMonitor
        {
            public int Counter { get; set; }
            public FileSystemWatcher Watcher { get; set; }
            public FileMonitor(string path)
            {
                Counter = 0;
                Watcher = new FileSystemWatcher();
                Watcher.Path = path;
                Watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
               | NotifyFilters.FileName | NotifyFilters.DirectoryName;
                Watcher.Filter = "*.csv";
                Watcher.Changed += (s, e) => OnChanged();
    
                Watcher.EnableRaisingEvents = true;
            }
    
            private void OnChanged()
            {
                try
                {
                    Watcher.EnableRaisingEvents = false;
                    MessageBox.Show(Counter.ToString());
                    Counter = Counter + 1;
                }
                finally
                {
                    Watcher.EnableRaisingEvents = true;
                }
            }
        }