For Programmers: Free Programming Magazines  


Home > Archive > C# > June 2004 > fileSystemWatcher problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author fileSystemWatcher problem
zfeld

2004-06-24, 12:57 am

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 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();
}
}
}
Joep

2004-06-24, 12:57 am

Why they have not fixed this design flaw is a miracle to me, things have
been like this for at least 5 years now. Not your program is at fault, it's
the os that notifies only one listener.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com