473,594 Members | 2,757 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 5418
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
1024
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
1746
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 twice (the test file was a tiny txt file). I connected to the process with the VS.Net debugger and...
1
6106
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 so I need to stop that thread waiting for the file. I have hookde up a ManualResetEvent to the...
2
4505
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
2710
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 wrong iam doing. Thanks.
20
4442
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 found FileSystemWatcher class not very reliable and sometimes it behavies unexpectedly.I'm afriad that...
10
1695
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.
1
10407
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 out when I create a new file under the watch directory: 1) Using notepad - OnCreate event...
5
5570
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 closing the filewatcher, and then reconnects (and thus creates a new watcher), I believe I end up with...
0
7947
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
7880
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8255
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
8374
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
8010
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,...
1
5739
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
3868
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...
1
1486
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1217
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.