473,583 Members | 2,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple instances of FileSystemWatch er ??????

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.Le ngth; i++)
{
FileSystemWatch er myWatcher = new System.IO.FileS ystemWatcher();
myWatcher.Notif yFilter = NotifyFilters.D irectoryName |
NotifyFilters.F ileName | NotifyFilters.A ttributes;
myWatcher.Delet ed += new FileSystemEvent Handler(LogDele ted);
myWatcher.Path = sDirsToWatch[i];
myWatcher.Enabl eRaisingEvents = true;
}

Now when it gets to either the .Path line or to when it gets to the
..EnableRaising Events 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 FileSystemWatch er
object ?

Any help would be most appreciated.

Thanks

David

Nov 17 '05 #1
13 9148
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.co m

"David" <da************ **@gmail.com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.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.Le ngth; i++)
{
FileSystemWatch er myWatcher = new System.IO.FileS ystemWatcher();
myWatcher.Notif yFilter = NotifyFilters.D irectoryName |
NotifyFilters.F ileName | NotifyFilters.A ttributes;
myWatcher.Delet ed += new FileSystemEvent Handler(LogDele ted);
myWatcher.Path = sDirsToWatch[i];
myWatcher.Enabl eRaisingEvents = true;
}

Now when it gets to either the .Path line or to when it gets to the
.EnableRaisingE vents 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 FileSystemWatch er
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.co m

"David" <da************ **@gmail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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 FileSystemWatch er.
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
FileSystemWatch er objects named: myWatcher0, myWatcher1.

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

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

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.Le ngth; i++)
{
FileSystemWatch er myWatcher = new System.IO.FileS ystemWatcher();
myWatcher.Notif yFilter = NotifyFilters.D irectoryName |
NotifyFilters.F ileName | NotifyFilters.A ttributes;
myWatcher.Delet ed += new FileSystemEvent Handler(LogDele ted);
myWatcher.Path = sDirsToWatch[i];
myWatcher.
myWatcher.Enabl eRaisingEvents = 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******** *************@g 14g2000cwa.goog legroups.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.Le ngth; i++)
{
FileSystemWatch er myWatcher = new System.IO.FileS ystemWatcher();
myWatcher.Notif yFilter = NotifyFilters.D irectoryName |
NotifyFilters.F ileName | NotifyFilters.A ttributes;
myWatcher.Delet ed += new FileSystemEvent Handler(LogDele ted);
myWatcher.Path = sDirsToWatch[i];
myWatcher.Enabl eRaisingEvents = true;
}

Now when it gets to either the .Path line or to when it gets to the
.EnableRaisingE vents 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 FileSystemWatch er
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" ;

FileSystemWatch er 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();
FileSystemWatch er watcher;

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

Then access them like so
watcher = (FileSystemWatc her)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.Enabl eRaisingEvents = true; it throws an
error.

Here is the code exactly:

int i;
string sAppRead = @"C:\FileWatche rTest2, C:\FileWatcherT est";

string [] sDirsToWatch;

sDirsToWatch = sAppRead.Split( ',');

ArrayList FileWatchers = new ArrayList();

for (i = 0; i < sDirsToWatch.Le ngth; i++)
{
FileSystemWatch er myWatcher = new System.IO.FileS ystemWatcher();
myWatcher.Notif yFilter = NotifyFilters.D irectoryName |
NotifyFilters.F ileName | NotifyFilters.A ttributes;
myWatcher.Delet ed += new FileSystemEvent Handler(LogDele ted);
myWatcher.Path = sDirsToWatch[i];
myWatcher.Enabl eRaisingEvents = true;

FileWatchers.Ad d(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.Enabl eRaisingEvents = true;

An unhandled exception of type 'System.IO.File NotFoundExcepti on'
occurred in system.dll

Additional information: Error reading the C:\FileWatcherT est
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

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

Similar topics

1
2204
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 currently separate SQL Server's onto one machine with two CPU's. Of the current two servers, one is for OLTP (~800Mb) and the other for
1
5978
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 original service under a new name but the SCM complains that the executable does not have this service implemented. Please note that I need to have...
11
5280
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 should configured to work, how memory is shared or not, etc. I can not seem to find any good links to this information. Thanks, Mike
4
3710
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 run the db2cc to see the command center I do not see any instances. All I see in the left window is the top level directory. Is this normal? How can...
12
3214
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 each fund, and a desire by the users to see several funds presented side-by-side. Is opening, say, five instances of the same form...
1
1919
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 procedures that check a new record before it's allowed into the database. She said that this happened on a day when her computer behaved strangely in...
11
20902
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 executable I can not run another instance as long as the first instance is running. How can I change this behavior? Thanks.
1
2975
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 program with their own ini file and multiple instances can run simultaneouly. Now what i want to convert the whole thing into a windows ervice that is...
4
3138
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
6906
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 user actions, I want to create n number instances of those classes, which is obviously unknown at compile time. What is the easiest way to create...
0
7888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8159
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8314
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8185
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6571
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3836
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1416
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.