-2

I'm trying to use the solution found here [Create multiple instances of the same FileSystemWatcher to make FileSystemWatchers on the fly but the Watcher_Created event doesn't seem to be triggered. Any thoughts as to what I am doing wrong?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;


namespace ConsoleApplication2
{
class Program
{

    static void Main(string[] args)
    {
        Console.WriteLine("Type Enter to exit:::");
        StartWatchers();
        Console.ReadLine();

    }

    public static void StartWatchers()
    {
        string[] ArrayPaths = new string[2];
        List<FileSystemWatcher> watchers = new List<FileSystemWatcher>();
        ArrayPaths[0] = @"\\WifeyPC\c$\User\Wifey\Desktop";
        ArrayPaths[1] = @"\\HubbyPC\c$\Users\Hubby\Desktop";

        int i = 0;
        foreach (String String in ArrayPaths)
        {
            watchers.Add(MyWatcherFatory(ArrayPaths[i]));
            i++;
        }

        foreach (FileSystemWatcher watcher in watchers)
        {
            watcher.EnableRaisingEvents = true; ;
            Console.WriteLine("Watching this folder {0}", watcher.Path);
            i++;
        }

    }
    FileSystemWatcher MyWatcherFatory(string path)
    {
        FileSystemWatcher watcher = new FileSystemWatcher(path);
        watcher.Changed += Watcher_Created;
        watcher.Path = path;
        watcher.Filter = "*.txt";
        watcher.IncludeSubdirectories = true;
        return watcher;
    }

    private void Watcher_Created(object sender, FileSystemEventArgs e)
    {
        System.Threading.Thread.Sleep(1000);
        FileInfo fileInfo = new FileInfo(e.FullPath);
        Console.WriteLine("File Created!! :: {0}", e.FullPath);
    }


}
}
  • 2
    Just by looking at your `console` app code, I don't see anything that would fire that event nor create watchers, from [`Main`](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/hello-world-your-first-program). Hth. – EdSF Oct 03 '17 at 00:39
  • Your updated code does not cause the error you describe, it should now be causing a compiler error instead of a runtime problem. – Scott Chamberlain Oct 03 '17 at 16:26

2 Answers2

0

You are never calling the StartWatchers method in your Main starting point.

Change the code as seen below:

static void Main(string[] args)
{
    Console.WriteLine("Type Enter to exit:::");
    StartWatchers();
    Console.ReadLine();
}

You're also going to need to change your Watcher_Created method as seen below. You had the ending double quote after fullPath which would just display that entire string, when you need to place the double quote after the parameter {0}:

 private void Watcher_Created(object sender, FileSystemEventArgs e)
{
    System.Threading.Thread.Sleep(1000);
    FileInfo fileInfo = new FileInfo(e.FullPath);
    Console.WriteLine("File Created!! :: {0}", e.FullPath);
}
Darthchai
  • 757
  • 8
  • 17
  • Thank you @Darthchai, I had at one point put the call to StartWatchers(); in my Main class, but it didn't like that. I added it again, like you suggested. It gave me error CS0120 becuase the Program.StartWatchers()is non-static, so I changed it to public static void. Now it's not liking MyWatcherFactory. (I edited my original post to show the entire new code.) – Drew Courtney Oct 03 '17 at 16:14
  • @DrewCourtney you changed your code, but the text you have with it no-longer describes your problem. You need to update all of your question. That being said, all of the functions need to be declared static for your code to work, you don't `new` any classes in your code so ***all*** function calls must be static. – Scott Chamberlain Oct 03 '17 at 16:23
0

You are 100% correct @scott-chamberlain. Below is the answer:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;


namespace ConsoleApplication2
{
    class Program
{

    static void Main(string[] args)
    {
        Console.WriteLine("Type Enter to exit:::");
        StartWatchers();
        Console.ReadLine();

    }

    public static void StartWatchers()
    {
        string[] ArrayPaths = new string[2];
        List<FileSystemWatcher> watchers = new List<FileSystemWatcher>();
    ArrayPaths[0] = @"\\WifeyPC\c$\User\Wifey\Desktop";
    ArrayPaths[1] = @"\\HubbyPC\c$\Users\Hubby\Desktop";top";

        int i = 0;
        foreach (String String in ArrayPaths)
        {
            watchers.Add(MyWatcherFatory(ArrayPaths[i]));
            i++;
        }

        foreach (FileSystemWatcher watcher in watchers)
        {
            watcher.EnableRaisingEvents = true; ;
            Console.WriteLine("Watching this folder {0}", watcher.Path);
            i++;
        }

    }
    public static FileSystemWatcher MyWatcherFatory(string path)
    {
        FileSystemWatcher watcher = new FileSystemWatcher(path);
        watcher.Changed += Watcher_Created;
        watcher.Path = path;
        watcher.Filter = "*.txt";
        watcher.IncludeSubdirectories = true;
        return watcher;
    }

    private static void Watcher_Created(object sender, FileSystemEventArgs e)
    {
        System.Threading.Thread.Sleep(1000);
        FileInfo fileInfo = new FileInfo(e.FullPath);
        Console.WriteLine("File Created!! :: {0}", e.FullPath);
    }


}
}