473,287 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Multiple instances of FileSystemWatcher ??????

I have been working on trying to write a directory watcher service.
One of the requirments is that it be able to watch multiple
directories, not sub directories of one parent directory, but just
multiple directories.

I have hit a snag and don't know how to get around it.

Basically I read in a list of directories from the app.config and stuff
them into an array, so that I have something like this:
sDirsToWatch[0] = "C:\Temp"
sDirsToWatch[1] = "D:\Temp"

Then I do the following

For (i = 0; i < sDirsToWatch.Length; i++)
{
FileSystemWatcher myWatcher = new System.IO.FileSystemWatcher();
myWatcher.NotifyFilter = NotifyFilters.DirectoryName |
NotifyFilters.FileName | NotifyFilters.Attributes;
myWatcher.Deleted += new FileSystemEventHandler(LogDeleted);
myWatcher.Path = sDirsToWatch[i];
myWatcher.EnableRaisingEvents = true;
}

Now when it gets to either the .Path line or to when it gets to the
..EnableRaisingEvents for the 2nd directory, I get an error: Error
reading the D:\Temp directory

I think what is happening is that all of the objects that I am creating
are named the same, so its causing problems.

So my question is, how do I dynamically name my FileSystemWatcher
object ?

Any help would be most appreciated.

Thanks

David

Nov 17 '05 #1
13 9092
David,

It doesn't look like this is the case. Are you running this in a
service? Are you sure that you have access to the drive itself (if it is a
service, then it might not, by default). Also, are you sure that the
directory itself exists?

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"David" <da**************@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I have been working on trying to write a directory watcher service.
One of the requirments is that it be able to watch multiple
directories, not sub directories of one parent directory, but just
multiple directories.

I have hit a snag and don't know how to get around it.

Basically I read in a list of directories from the app.config and stuff
them into an array, so that I have something like this:
sDirsToWatch[0] = "C:\Temp"
sDirsToWatch[1] = "D:\Temp"

Then I do the following

For (i = 0; i < sDirsToWatch.Length; i++)
{
FileSystemWatcher myWatcher = new System.IO.FileSystemWatcher();
myWatcher.NotifyFilter = NotifyFilters.DirectoryName |
NotifyFilters.FileName | NotifyFilters.Attributes;
myWatcher.Deleted += new FileSystemEventHandler(LogDeleted);
myWatcher.Path = sDirsToWatch[i];
myWatcher.EnableRaisingEvents = true;
}

Now when it gets to either the .Path line or to when it gets to the
.EnableRaisingEvents for the 2nd directory, I get an error: Error
reading the D:\Temp directory

I think what is happening is that all of the objects that I am creating
are named the same, so its causing problems.

So my question is, how do I dynamically name my FileSystemWatcher
object ?

Any help would be most appreciated.

Thanks

David

Nov 17 '05 #2
Nicholas:

Not running this as a service yet, until I get the code nailed down I
am doing it from a Windows Form application, but it will be moved to a
service once I get the code working.

Both directories exist on my local hard drive and I can freely copy and
delete files from them.

Thanks for the help

David

Nov 17 '05 #3
David,

I don't think you can have one watcher watching multiple directories.
Try creating a new instance for each directory you would like to watch.

I think the problem is that everytime you hit the top of your loop, you
make myWatcher point to a new object, and the old one gets destoryed
since nothing is referencing it any longer.

Andy

Nov 17 '05 #4
David,

The naming of the file system watchers is not an issue, since you create
a new one each time you iterate through the loop. Are there any more
details to the exception?

If you want to hold all the instances, you can create an array of file
system watchers, and populate each element of the array with a reference to
a different instance.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"David" <da**************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Nicholas:

Not running this as a service yet, until I get the code nailed down I
am doing it from a Windows Form application, but it will be moved to a
service once I get the code working.

Both directories exist on my local hard drive and I can freely copy and
delete files from them.

Thanks for the help

David

Nov 17 '05 #5
Andy:

I agree with what you are saying, I'm pretty sure that is what the
problem is.

What I don't know how to do is dynamically name the FileSystemWatcher.
Since I am using a for statement, I was hoping I could somehow derive
the name of the object every time the loop iterates, so for example
since there are only 2 directories in the array, I would end up with
FileSystemWatcher objects named: myWatcher0, myWatcher1.

But if I try and build a string for the FileSystemWatcher object name
and then reference that variable on the line where I say
FileSystemWatcher myWatcher = new System.IO.FileSystemWatcher();, I get
an error during compile time.

So I guess that the question is given this line of code:
FileSystemWatcher myWatcher = new System.IO.FileSystemWatcher();

How do I dynamically name the object myWatcher so that when it iterates
through the for, its not always the same name ?

Nov 17 '05 #6
Hi,

I would change a little your code:

ArrayList watchers = new ArrayList();

For (i = 0; i < sDirsToWatch.Length; i++)
{
FileSystemWatcher myWatcher = new System.IO.FileSystemWatcher();
myWatcher.NotifyFilter = NotifyFilters.DirectoryName |
NotifyFilters.FileName | NotifyFilters.Attributes;
myWatcher.Deleted += new FileSystemEventHandler(LogDeleted);
myWatcher.Path = sDirsToWatch[i];
myWatcher.
myWatcher.EnableRaisingEvents = true;
watchers.Add( myWatcher);
}

Try it, basically I'm keeping the references to them. ( in case you need to
disable one afterwards )

Also, and here is where your error may be, rewrite it like this:

sDirsToWatch[0] = @"C:\Temp";
sDirsToWatch[1] = @"D:\Temp";

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

"David" <da**************@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I have been working on trying to write a directory watcher service.
One of the requirments is that it be able to watch multiple
directories, not sub directories of one parent directory, but just
multiple directories.

I have hit a snag and don't know how to get around it.

Basically I read in a list of directories from the app.config and stuff
them into an array, so that I have something like this:
sDirsToWatch[0] = "C:\Temp"
sDirsToWatch[1] = "D:\Temp"

Then I do the following

For (i = 0; i < sDirsToWatch.Length; i++)
{
FileSystemWatcher myWatcher = new System.IO.FileSystemWatcher();
myWatcher.NotifyFilter = NotifyFilters.DirectoryName |
NotifyFilters.FileName | NotifyFilters.Attributes;
myWatcher.Deleted += new FileSystemEventHandler(LogDeleted);
myWatcher.Path = sDirsToWatch[i];
myWatcher.EnableRaisingEvents = true;
}

Now when it gets to either the .Path line or to when it gets to the
.EnableRaisingEvents for the 2nd directory, I get an error: Error
reading the D:\Temp directory

I think what is happening is that all of the objects that I am creating
are named the same, so its causing problems.

So my question is, how do I dynamically name my FileSystemWatcher
object ?

Any help would be most appreciated.

Thanks

David

Nov 17 '05 #7
Dave,

I think what you're saying you want is to name the object using the
value stored in a string.

Something like:
string nameIt = "watcher01";

FileSystemWatcher nameIt = new ...

If thats what you want, you can't really do that..however you could
store your watcher objects in a HashTable.

HashTable watchers = new HashTable();
FileSystemWatcher watcher;

for( int i = 0 ; i < 2 ; i += 1 0 {
watcher = new FileSystemWatcher();
// set its properties
watchers.Add( "watcher" + i.ToSring(), watcher );
}

Then access them like so
watcher = (FileSystemWatcher)watchers[ "watcher1" ];

Andy

Nov 17 '05 #8
Ignacio:

Thanks for the reply, I did try that originally, but that doesn't seem
to work either, basically on the 2nd interation into the loop when it
gets to the line for myWatcher.EnableRaisingEvents = true; it throws an
error.

Here is the code exactly:

int i;
string sAppRead = @"C:\FileWatcherTest2, C:\FileWatcherTest";

string [] sDirsToWatch;

sDirsToWatch = sAppRead.Split(',');

ArrayList FileWatchers = new ArrayList();

for (i = 0; i < sDirsToWatch.Length; i++)
{
FileSystemWatcher myWatcher = new System.IO.FileSystemWatcher();
myWatcher.NotifyFilter = NotifyFilters.DirectoryName |
NotifyFilters.FileName | NotifyFilters.Attributes;
myWatcher.Deleted += new FileSystemEventHandler(LogDeleted);
myWatcher.Path = sDirsToWatch[i];
myWatcher.EnableRaisingEvents = true;

FileWatchers.Add(myWatcher);
}

Nov 17 '05 #9
Nicholas:

Are you sure the naming of the file system watcher is not an issue ?
It would seem to me now that I have the creation code in a loop, that
everytime the loop iterates a object gets created, but it has the same
name as the one that just got created, so doesn't that mangle the first
one ?

For instance: I create a object called myWatcher, set all of its
properties and enable it to start watching, then the loop starts over
and I create a new object called myWatcher, what has happened to the
first on that was created ?

Here is the exact message from the development environment when it hits
the line myWatcher.EnableRaisingEvents = true;

An unhandled exception of type 'System.IO.FileNotFoundException'
occurred in system.dll

Additional information: Error reading the C:\FileWatcherTest
directory.
From this text, I would think that the directory doesn't exist, but I

know it does. I just created it.

Thanks

David

Nov 17 '05 #10
Andy:

Yep, that is what I was wanting to do, If I manually create two objects
with code and name them completely different, then this works fine, so
what you are describing with the nameIt string is what I was wanting.

I tried your Hashtable example, I end up with the same error.

Heres what I tried:

int i;
string sAppRead = @"C:\FileWatcherTest, C:\FileWatcherTest";
string[] sDirsToWatch;

sDirsToWatch = sAppRead.Split(',');

Hashtable watchers = new Hashtable();
FileSystemWatcher watcher;
FileSystemWatcher watcher1;

for (i = 0; i < sDirsToWatch.Length; i++)
{
watcher = new FileSystemWatcher();
watcher.NotifyFilter = NotifyFilters.DirectoryName |
NotifyFilters.FileName | NotifyFilters.Attributes;
watcher.Deleted += new FileSystemEventHandler(LogDeleted);
watcher.Path = sDirsToWatch[i];

watchers.Add("watcher" + i.ToString(), watcher);
}

watcher = (FileSystemWatcher)watchers["watcher0"];
watcher.EnableRaisingEvents = true;

watcher1 = (FileSystemWatcher)watchers["watcher1"];
watcher1.EnableRaisingEvents = true;

Nov 17 '05 #11

string sAppRead = @"C:\FileWatcherTest2, C:\FileWatcherTest";

The problem lies in that it cannot find the folder named "
c:\FileWatcherTest": note the space.

Either use
string sAppRead = @"C:\FileWatcherTest2,C:\FileWatcherTest";
without a space after the comma or trim the folderstring.

myWatcher.Path = sDirsToWatch[i].Trim();
Nov 17 '05 #12
Hey David,

If you're only getting the error on the second watcher, I suspect its
because of the space after the comma in your path list. Try removing
the space, maybe the watcher class doesn't like having it as the first
character in the path to watch.

andy

Nov 17 '05 #13
Andy:

You are correct, I was totally missing the space in front of the second
string.

So actually the directory didn't exist.

Thanks for the help.

Nov 17 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Martin V. | last post by:
Hello, Is the multiple instances capability of SQL2K mature enough for a Production system? We're looking at upgrading our hardware but the proposed solution calls for consolidating two...
1
by: Vlad | last post by:
Is there any way to install multiple instances of the same windows service designed with VS.NET 2003? I tried copying the binaries into a separate folder and then copying registry entries for the...
11
by: Mike | last post by:
Looking to find any information on how to properly configure multiple instances of DB2. This is on Win2k db2 ver 7.2. I am basically looking for information on how the multiple instance settings...
4
by: Yasaswi Pulavarti | last post by:
I have a Aix 5.2 server with DB2 UDB 8.1 I created three different instances using the db2setup utility three times. The default instance is db2inst1. When I su - db2inst2 and su - db2inst3 and...
12
by: (Pete Cresswell) | last post by:
I know I can open many instances of a given form, but I've never done it. Now I'm analyzing an application where that seems like just the ticket: Many investment funds, *lots* of data points for...
1
by: Johan | last post by:
If a user happens to open multiple instances of her frontend, what kind of problems can occur? For example, one user reported that she was able to, once or twice, bypass the evaluation...
11
by: Clark Stevens | last post by:
I just finished a WinForms app in VB.NET. I want to allow the user to be able to run multiple instances of the program like you can with Notepad and Wordpad. The way it is now, once I run the...
1
by: sunil | last post by:
Hello All. I have written a program as an exe that performs some kind of order processing. The program is first configured and then started manually. I have have multiple instances of this...
4
by: Mike | last post by:
Class A public objX I want to create 2 or more instances of Class A and have the same value for objX in all instances. Instance1 of Class A Instance2 of Class A Instance3 of Class A
6
by: Bugs | last post by:
Does anyone have any recommendations on the best way to create multiple instances of the same class when the final number of instances is unknown? For example, I have a class and based on some...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.