473,807 Members | 2,853 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FileSystemWatch er 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 {

[STAThreadAttrib ute()]

public static void Main() {
File.Delete("c: \\test");
using (FileSystemWatc her watch = new FileSystemWatch er()) {
watch.Path = "c:\\";
watch.Filter = "test";
watch.Created += new FileSystemEvent Handler(OnChang ed);
watch.Deleted += new FileSystemEvent Handler(OnChang ed);
watch.Changed += new FileSystemEvent Handler(OnChang ed);
watch.EnableRai singEvents = true;
}
FileStream oFs = new FileStream("c:\ \test", FileMode.Create New,
FileAccess.Read Write);
StreamWriter oWriter = new StreamWriter(oF s);
oWriter.Flush() ;
oWriter.Close() ;
oFs.Close();
File.Delete("c: \\test");
Application.Run (new Form());
}

static void OnChanged(objec t sender, FileSystemEvent Args e) {
MessageBox.Show (e.FullPath + e.ChangeType.To String());
}
}
Jun 15 '07 #1
4 7758
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.co m>
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 FileSystemWatch er 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******** *****@TK2MSFTNG P06.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 {

[STAThreadAttrib ute()]

public static void Main() {
File.Delete("c: \\test");
using (FileSystemWatc her watch = new FileSystemWatch er()) {
watch.Path = "c:\\";
watch.Filter = "test";
watch.Created += new FileSystemEvent Handler(OnChang ed);
watch.Deleted += new FileSystemEvent Handler(OnChang ed);
watch.Changed += new FileSystemEvent Handler(OnChang ed);
watch.EnableRai singEvents = true;
}
FileStream oFs = new FileStream("c:\ \test", FileMode.Create New,
FileAccess.Read Write);
StreamWriter oWriter = new StreamWriter(oF s);
oWriter.Flush() ;
oWriter.Close() ;
oFs.Close();
File.Delete("c: \\test");
Application.Run (new Form());
}

static void OnChanged(objec t sender, FileSystemEvent Args e) {
MessageBox.Show (e.FullPath + e.ChangeType.To String());
}
}

Jun 15 '07 #3
Andy Bates <an**@ussdev.co mwrote:
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 FileSystemWatch er 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.co m>
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.co mwrote in message
news:MP******** *************@m snews.microsoft .com...
Andy Bates <an**@ussdev.co mwrote:
>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 FileSystemWatch er 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.co m>
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
9122
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 copying a large file into the watched folder, the .Created event is fired off immediately and not when the file is finished copying. I have tried waiting for a certain number of .Changed events to be fired after the initial .Created event, and...
0
3011
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 a week. Then suddenly the monitored network server starts to behave crazily. It sends to my Sync Server dozens of replicated file change events. Say one file was changed two days ago, but the monitored server still keeps sending me many many...
3
4604
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 a problem: I wonna get shure, that really all files are processed and I need to be shure, that no file is processed twice. If my application is started while there are already files in the directory (which is realistic, as the application could...
3
14314
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 a working thread for processing the file, created a new FileSystemWatcher (as he said for real time processing), and then called the join method for the first thread. I can't really see the sence in this. Aren't the events of the...
10
1719
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. if there are other noobs here like me feel free to use my code if it's of any use :) I've set a breakpoint at doWorkOnChangedFile but notinh happens... i'm flooding my log like hell.
2
4553
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 array (the number of instances i need is dynamic). Here is the code I wrote: Dim arrFSW() As FileSystemWatcher ReDim arrFSW(lstFiles.Items.Count - 1) For i As Integer = 0 To lstFiles.Items.Count - 1
12
7463
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
2366
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 their events in different threads. I found plenty of examples on the Web of how to "detangle" the threads, using Delegate functions, .InvokeRequired, and .Invoke. But all of these examples demonstrated how to do it on a single form. I wanted...
1
1889
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 application, which uses other class libraries. One lib, called Utils.dll contains custom collection implementation. This simple collection just overrides Add and Remove methods to raise
0
9720
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
10626
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
10372
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
10374
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
10112
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
9193
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5546
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5685
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.