473,832 Members | 2,156 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 9198
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
2217
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
6007
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 distinct instances of executables installed not merely multiple windows services defined within...
11
5330
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
3722
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 I see all the instances? Also how can I start all three instances? Please inform me. Thanks,
12
3242
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 real-world-doable? -- PeteCresswell
1
1926
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 general. We have not been able to repeat/reconstruct the error, and I wonder if the explanation can be...
11
21016
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
2990
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 self starting and reads the configuration from the .ini file(its own) and can be run separately as a...
4
3157
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
6924
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 and then manage these kinds of objects? Using an array seems intuitive but it also seems like it...
0
9795
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 usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
10781
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10499
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10541
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10212
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5789
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4421
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3972
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3078
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.