Multiple FileSystemWatchers

K

Ken Madden

I want to create multiple fileSystemWatchers in a Windows Service to
constantly watch multiple folders for file creations and then execute
certain code based on these actions. The problem is that these directories
are not part of the same directory so I cannot use the subfolder flag. The
number of folders also has to be dynamic so I cannot simply create a certain
number of FileSystemWatchers with different paths. I am going to pull a
list of paths from an XML config file and create a FilesystemWatcher for
each flag. I tried creating a FilesystemWatcher in a thread thinking that I
could simply iterate through the paths creating an FSW for each path. But
when I do this, the FSW gets created, verified through Event log entries,
but nothing happens when a file is created.

Can anyone point me in the right direction? I cannot find any articles
regarding this. I found one article that really seemed like what I was
trying to do but, of course, the article was no longer available. :(

Thanks,

Ken
 
I

Ignacio Machin

Hi Ken,

I have a windows service with three FileSystemWatchers and it works fine, I
create them in the thread of the service ( the one I create in the OnStart
method )
I think that there is nothing wrong on create multiple FileSystemWatchers,
you can insert them on a ArrayList with each one watching a particular
directory, remember that it's important to keep a reference to the object
( that's why you add it to a ArrayList ) otherwise it gets GC'ed.

Other than that I cannot think of nothing else to do, please just post back
if you need some code.

Hope this help,
 
K

Ken Madden

I would love to see a code sample. The first thing I tried was an array but
was not able to create an array of FileSystemWatchers. That seems like that
would be the ideal way to do it for my purposes.

Thanks,

Ken
 
I

Ignacio Machin

Hi Ken,

I just assembled the code below, you will need to improve it, it will just
point you in the right direction , I cahnged a file in the inetpub directory
and it worked almost fine, almost fine cause it raise two times the same
event, take a look into it and tell me if you solved your problem, I will
look into that double event thing later this afternoon.

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

ArrayList fw= new ArrayList();

protected void CreateWatchers()

{

string [] strs = new string[] {@"c:\temp",@"c:\projects", @"c:\inetpub" };

foreach( string s in strs)

{

FileSystemWatcher Clientwatcher= new FileSystemWatcher();

Clientwatcher.Path = s;

Clientwatcher.Filter = "*.flag";

Clientwatcher.NotifyFilter = NotifyFilters.LastWrite;

Clientwatcher.Created += new FileSystemEventHandler( ClientFileUpdated);

Clientwatcher.Changed += new FileSystemEventHandler( ClientFileUpdated);

Clientwatcher.EnableRaisingEvents =true;

fw.Add( Clientwatcher);

}

}

public void ClientFileUpdated(object source, FileSystemEventArgs e)

{

object o = source;

}
 
K

Ken Madden

Beautiful. My implementation was slightly off. You put me on the right
track. I got my multiple listeners running off a two path array. Now I can
scale that out to however many paths are stored in my XML config pretty
easily. Thank you thank you. I have been beating my head against the wall.

As far as the double events, that is common when using the changed and
created event captures. If you create a new file in that directory, rather
than just copy one in from somewhere else, a create event is raised when the
file is created. But it is really only creating a placeholder at that point.
then the content is written to the file which raises the changed event. I
had to deal with that on a different utility I wrote a while back. I
basically just worked around it by detecting the create, sleeping for two
minutes, and then processing the xml log file. I am sure this is not ideal
but it worked for that particular application. I have found plenty of docs
on the web regarding this problem but not a lot of soltuions.

Thanks again,

Ken

Ignacio Machin said:
Hi Ken,

I just assembled the code below, you will need to improve it, it will just
point you in the right direction , I cahnged a file in the inetpub directory
and it worked almost fine, almost fine cause it raise two times the same
event, take a look into it and tell me if you solved your problem, I will
look into that double event thing later this afternoon.

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

ArrayList fw= new ArrayList();

protected void CreateWatchers()

{

string [] strs = new string[] {@"c:\temp",@"c:\projects", @"c:\inetpub" };

foreach( string s in strs)

{

FileSystemWatcher Clientwatcher= new FileSystemWatcher();

Clientwatcher.Path = s;

Clientwatcher.Filter = "*.flag";

Clientwatcher.NotifyFilter = NotifyFilters.LastWrite;

Clientwatcher.Created += new FileSystemEventHandler( ClientFileUpdated);

Clientwatcher.Changed += new FileSystemEventHandler( ClientFileUpdated);

Clientwatcher.EnableRaisingEvents =true;

fw.Add( Clientwatcher);

}

}

public void ClientFileUpdated(object source, FileSystemEventArgs e)

{

object o = source;

}


Ken Madden said:
I would love to see a code sample. The first thing I tried was an array but
was not able to create an array of FileSystemWatchers. That seems like that
would be the ideal way to do it for my purposes.

Thanks,

Ken

fine,
pull
a thinking
that path.
But
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top