My program has two instances of a control have fileSystemWatchers that
are set to monitor the same directory for changes. When they are
watching a directory on the local machine, it works fine, two events
are received. However, when I am watching a directory on a machine
over the network by setting the fileSystemWatchers' path to i.e.
//anotherMachine/c/aDir then only one instance of the
fileSystemWatcher receives a FileSystemEvent. I have dug around and
have seen that others have had this problem. The only suggestions was
to have a single watcher that passes on the event to the two objects
that require it, a hack. Both the remote machine and the local machine
are running XP pro.
I am sorry if this is a double post, I just never saw my first post show up.
I wrote a sample console app that displays the same problem.
using System;
using System.IO;
using System.Collections;
namespace fileWatcherProgram
{
public class WatcherInstance
{
FileSystemWatcher watcher;
public WatcherInstance(string path)
{
watcher = new FileSystemWatcher();
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
watcher.Path= path;
watcher.NotifyFilter = NotifyFilters.DirectoryName | NotifyFilters.FileName;
watcher.Filter = ""; // watch all files
watcher.IncludeSubdirectories = true; // look at all subdirectories
watcher.EnableRaisingEvents = true; // fire off the watcher
}
private void OnChanged(object source, FileSystemEventArgs e)
{
Console.WriteLine("File: {0} {1}", e.FullPath, e.ChangeType.ToString("G"));
}
private void OnRenamed(Object source, RenamedEventArgs e)
{
Console.WriteLine("File: {0} Renamed to {1}", e.OldFullPath,
e.FullPath);
}
}
public class Watcher
{
[STAThread]
public static void Main(String[] args)
{
if(args.Length < 1)
{
Console.WriteLine("Usage: Watcher.exe <directory>");
}
else
{
ArrayList FileSystemWatchers = new ArrayList();
for(int i=0; i<2; i++)
{
WatcherInstance watcher = new WatcherInstance(args[0]);
FileSystemWatchers.Add(watcher);
}
}
Console.WriteLine("Press Enter to quit the sample\r\n");
Console.ReadLine();
}
}
}