473,465 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

FileSystemWatcher does not raise events

To reporoduce, run the code.

Observed: Form is shown

Expected: message box should displayed.

How to fix ?

Andrus.

using System;
using System.Windows.Forms;
using System.IO;

class main {

[STAThreadAttribute()]

public static void Main() {
File.Delete("c:\\test");
using (FileSystemWatcher watch = new FileSystemWatcher()) {
watch.Path = "c:\\";
watch.Filter = "test";
watch.Created += new FileSystemEventHandler(OnChanged);
watch.Deleted += new FileSystemEventHandler(OnChanged);
watch.Changed += new FileSystemEventHandler(OnChanged);
watch.EnableRaisingEvents = true;
}
FileStream oFs = new FileStream("c:\\test", FileMode.CreateNew,
FileAccess.ReadWrite);
StreamWriter oWriter = new StreamWriter(oFs);
oWriter.Flush();
oWriter.Close();
oFs.Close();
File.Delete("c:\\test");
Application.Run(new Form());
}

static void OnChanged(object sender, FileSystemEventArgs e) {
MessageBox.Show(e.FullPath + e.ChangeType.ToString());
}
}
Jun 15 '07 #1
4 7726
Andrus <ko********@hot.eewrote:
To reporoduce, run the code.

Observed: Form is shown

Expected: message box should displayed.

How to fix ?
You've surrounded the use of "watch" with a using statement, so it's
being disposed immediately. It doesn't get a chance to actually do
anything.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 15 '07 #2
1. First statement File.Delete ("C:\\test") will throw if the file doesn't
exist.
2. Filter should be a file specification so probably "*.*" should be used in
preference to "test".
3. Also your FileSystemWatcher is inside a using statement which means that
upon completion of the block the object will be deleted as the Dispose
method will be called... meaning that no events will be raised!

Move the code outside the using block to the inside of it.

- Andy

"Andrus" <ko********@hot.eewrote in message
news:O2*************@TK2MSFTNGP06.phx.gbl...
To reporoduce, run the code.

Observed: Form is shown

Expected: message box should displayed.

How to fix ?

Andrus.

using System;
using System.Windows.Forms;
using System.IO;

class main {

[STAThreadAttribute()]

public static void Main() {
File.Delete("c:\\test");
using (FileSystemWatcher watch = new FileSystemWatcher()) {
watch.Path = "c:\\";
watch.Filter = "test";
watch.Created += new FileSystemEventHandler(OnChanged);
watch.Deleted += new FileSystemEventHandler(OnChanged);
watch.Changed += new FileSystemEventHandler(OnChanged);
watch.EnableRaisingEvents = true;
}
FileStream oFs = new FileStream("c:\\test", FileMode.CreateNew,
FileAccess.ReadWrite);
StreamWriter oWriter = new StreamWriter(oFs);
oWriter.Flush();
oWriter.Close();
oFs.Close();
File.Delete("c:\\test");
Application.Run(new Form());
}

static void OnChanged(object sender, FileSystemEventArgs e) {
MessageBox.Show(e.FullPath + e.ChangeType.ToString());
}
}

Jun 15 '07 #3
Andy Bates <an**@ussdev.comwrote:
1. First statement File.Delete ("C:\\test") will throw if the file doesn't
exist.
Nope - from the docs:

<quote>
Deletes the specified file. An exception is not thrown if the specified
file does not exist.
</quote>
2. Filter should be a file specification so probably "*.*" should be used in
preference to "test".
3. Also your FileSystemWatcher is inside a using statement which means that
upon completion of the block the object will be deleted as the Dispose
method will be called... meaning that no events will be raised!

Move the code outside the using block to the inside of it.
Indeed, that's the problem.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 15 '07 #4
Okay... my mistake :). You live and learn!

- Andy

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Andy Bates <an**@ussdev.comwrote:
>1. First statement File.Delete ("C:\\test") will throw if the file
doesn't
exist.

Nope - from the docs:

<quote>
Deletes the specified file. An exception is not thrown if the specified
file does not exist.
</quote>
>2. Filter should be a file specification so probably "*.*" should be used
in
preference to "test".
3. Also your FileSystemWatcher is inside a using statement which means
that
upon completion of the block the object will be deleted as the Dispose
method will be called... meaning that no events will be raised!

Move the code outside the using block to the inside of it.

Indeed, that's the problem.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jun 15 '07 #5

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

Similar topics

4
by: Josh Usovsky | last post by:
I'm setting up a watched folder using FileSystemWatcher. When I drop a small file into the watched folder, I can respond to a .Created event and process the file with other code. However, if I try...
0
by: cxw0106 | last post by:
Hello, I have some weird problem with the FileSystemWatcher. I have developed an application that monitors a network directory for file changes of a certain file type. The program runs well for...
3
by: Stampede | last post by:
Hi, I write an application which waits for incomming files in a specified directory. I thought, that using the FileSystemWatcher would be the best, as it does exactly what I need. But now I have...
3
by: Stampede | last post by:
Hi, I want to use the FileSystemWatcher in a Windows Service. I read an article, where the author created the FileSystemWatcher object in a seperate thread and when the event is fired, he started...
10
by: Kai Thorsrud | last post by:
Hi i'm an event Noob and i'm calling a class from my main module console app like this. I'm sorry if this is a lot of code to read but i can't see the error according to my book. Thanks a LOT. ...
2
by: DiGa | last post by:
Hi all I have to write an application in VB.NET which monitors multiple files at the time and notifies the user about changes. I thought to use multiple instances of FileSystemWatcher in an...
12
by: ljh | last post by:
Has anyone else noticed that the FileSystemWatcher raises the changed event twice when a file is changed? Do you have any idea why this is the case?
1
by: teslar91 | last post by:
I've been learning VB.NET for the past few weeks. One of the problems I've run into is difficulties updating controls in events from certain components, such as the FileSystemWatcher, that raise...
1
by: Asko Telinen | last post by:
Hi all. I ran into quite strange problem concerning the event raising inside FileSystemWatcher Delete event. First, i would like to describe a bit my environment. I have main GUI...
0
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,...
0
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...
1
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.