470,626 Members | 2,139 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,626 developers. It's quick & easy.

Multiple FileSystemWatchers

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
Nov 15 '05 #1
4 10623
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,

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

"Ken Madden" <os****@microsoft.com> wrote in message
news:eo*************@TK2MSFTNGP09.phx.gbl...
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

Nov 15 '05 #2
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

"Ignacio Machin" <ignacio.machin AT dot.state.fl.us> wrote in message
news:Or**************@TK2MSFTNGP09.phx.gbl...
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,

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

"Ken Madden" <os****@microsoft.com> wrote in message
news:eo*************@TK2MSFTNGP09.phx.gbl...
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


Nov 15 '05 #3
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" <os****@microsoft.com> wrote in message
news:eG**************@tk2msftngp13.phx.gbl...
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

"Ignacio Machin" <ignacio.machin AT dot.state.fl.us> wrote in message
news:Or**************@TK2MSFTNGP09.phx.gbl...
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,

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

"Ken Madden" <os****@microsoft.com> wrote in message
news:eo*************@TK2MSFTNGP09.phx.gbl...
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



Nov 15 '05 #4
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" <ignacio.machin AT dot.state.fl.us> wrote in message
news:OD**************@tk2msftngp13.phx.gbl...
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" <os****@microsoft.com> wrote in message
news:eG**************@tk2msftngp13.phx.gbl...
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

"Ignacio Machin" <ignacio.machin AT dot.state.fl.us> wrote in message
news:Or**************@TK2MSFTNGP09.phx.gbl...
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,

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

"Ken Madden" <os****@microsoft.com> wrote in message
news:eo*************@TK2MSFTNGP09.phx.gbl...
> 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
>
>



Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Rolf Wester | last post: by
66 posts views Thread by Darren Dale | last post: by
6 posts views Thread by Ben Hallert | last post: by
22 posts views Thread by Matthew Louden | last post: by
1 post views Thread by Fredrik Johansson | last post: by
35 posts views Thread by keerthyragavendran | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.