473,573 Members | 3,023 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FileSystemWatch er (WaitForChanged )

I have a windows service that monitors a folder. I have a
routine to process XML files whenever a new file gets
created (in the folder that the Filesystemwatch er is
monitoring.)
Since the files are large, before the copy process is
completed the Created event fires first, and I get errors
because I'm trying to read the xml file on to a stream
for further process.
The Created event gets raised when I copy and paste a new
xml file onto folder (but fires before the file is fully
copied). I need some help on how to use the
WaitForChanged method in this example.

Thanks
-Megan

sample code:
A Windows service loads this class and on it's
MyService.OnSta rt() sets the FSW's EnableRaisingEv ents.
(resets on MyService.OnSto p())

Class FSMonitor
private MyWatcher as FileSystemWatch er
Public Sub New()
MyWatcher = New FileSystemWatch er
With MyWatcher
.Path = "c:\MyFolde r"
.Filter = ".xml"
.IncludeSubDire ctories = False
.NotifyFilter = NotifyFilters.C reationTime Or
NotifyFilters.L astWrite OR etc..

AddHandler .Created AddressOf OnChanged()
End With

Private Sub OnChanged(byval obj as object, byval e as
FileSystemEvent Args)
If e.ChangeType = WatcherChangeTy pes.Created Then
'process large xml file
Try

Dim MyReader as New StreamReader(e. FullPath)
ProcessXMLFile( MyReader)

Catch ex as Exception
Debug.Writeline (ex.message)
Finally
End Try
End If
End Sub
End Class
Nov 20 '05 #1
1 5417
Hi Megan,

I am not sure you are still looking for answer but:

If you are not already using NTFS, you might benefit from its being
transactional. It means unless a file is completely copied, it practically
does not exist on the system. I am not sure about VFAT.

If it is already NTFS, you might use an approach like this:

Do Until PathFileExists( copiedFile)
Thread.Sleep(20 00)
Loop

Cheers
Ali

"Megan" <an*******@disc ussions.microso ft.com> wrote in message
news:05******** *************** *****@phx.gbl.. .
I have a windows service that monitors a folder. I have a
routine to process XML files whenever a new file gets
created (in the folder that the Filesystemwatch er is
monitoring.)
Since the files are large, before the copy process is
completed the Created event fires first, and I get errors
because I'm trying to read the xml file on to a stream
for further process.
The Created event gets raised when I copy and paste a new
xml file onto folder (but fires before the file is fully
copied). I need some help on how to use the
WaitForChanged method in this example.

Thanks
-Megan

sample code:
A Windows service loads this class and on it's
MyService.OnSta rt() sets the FSW's EnableRaisingEv ents.
(resets on MyService.OnSto p())

Class FSMonitor
private MyWatcher as FileSystemWatch er
Public Sub New()
MyWatcher = New FileSystemWatch er
With MyWatcher
.Path = "c:\MyFolde r"
.Filter = ".xml"
.IncludeSubDire ctories = False
.NotifyFilter = NotifyFilters.C reationTime Or
NotifyFilters.L astWrite OR etc..

AddHandler .Created AddressOf OnChanged()
End With

Private Sub OnChanged(byval obj as object, byval e as
FileSystemEvent Args)
If e.ChangeType = WatcherChangeTy pes.Created Then
'process large xml file
Try

Dim MyReader as New StreamReader(e. FullPath)
ProcessXMLFile( MyReader)

Catch ex as Exception
Debug.Writeline (ex.message)
Finally
End Try
End If
End Sub
End Class

Nov 20 '05 #2

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

Similar topics

0
1023
by: Balu | last post by:
Hi, I am using the FileSystemWatcher object to watch for a specific file being created in a directory (with a timeout): fileWatcherResult = fileWatcher.WaitForChanged (WatcherChangeTypes.Created, 180000); After the file is crated I try to open it.
0
1745
by: Erik Herje via .NET 247 | last post by:
Hi, I have a Windows Service written in C# whos purpose is to sniff a folder to look for changes in the folder. If a file is created or changed, it should get its name and start an object the sends the file to another server. The problem I have is that when for example a txt file gets edited and saved with notepad, OnChanged gets executed...
1
6105
by: Matthew Wieder | last post by:
The documentation specifies that the WaitForChanged method should not be called on the UI thread or it will freeze the UI thread. So, I have started it in a thread seperate from the main form's thread. The WaitForChanged event will wait infinitely until a file is created, but the user needs to be able to stop the application from running and...
2
4503
by: John Lee | last post by:
Hi, I wrote a small program that copy a list of files to a specified folder one by one, i.e. this app will copy one file into the specified folder and wait until it's consumed (deleted) then copy another one in. Here is what my code: // other codes to get a list of files while (morefiles) {
3
2709
by: Krish | last post by:
Hello Gurus, Pardon me for asking dumb question... I wrote small csharp program to watch a folder for any created text file... When i try to run through debugger and stop after " watcher.EnableRaisingEvents = true;", i manually drag and drop windows text file and this program doesnt seem to fire up... see code below and pl. advise what...
20
4439
by: J-T | last post by:
We are working on an asp.net application which is a 3-tier application.I was aksed to create a component which monitors a folder and gets the file and pass them to a class library in our business logic layer(so far so good and easy).I initialize my class which is using a FileSystemWatcher in my Global.asax and everything works fine.I have...
10
1693
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...
1
10397
by: Abel Chan | last post by:
Hi there, I was trying to write a simple NT services using .NET 2.0 and fileSystemWatcher control. The goal is to poll documents from a watch directory and ftp them to a remote web site. I created a Windows Services project using VS2005. Drag a fileSystemWatcher control to the designer and added the following code. Here is what I found...
5
5563
by: =?Utf-8?B?Sm9obiBT?= | last post by:
I am trying to find out if there is a way to tell if there is already a filesystemwatcher (created by a webservice) monitoring a folder. I have a webservice that creates a filesystemwatcher, monitors a folder and then returns the contents of the new/changed files. However, if the client app loses connection to the webservice without...
0
7783
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
8029
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
6424
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...
1
5600
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...
0
5293
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
3734
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...
0
3735
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2222
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
0
1044
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.