473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

FileSystemWatcher (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 Filesystemwatcher 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.OnStart() sets the FSW's EnableRaisingEvents.
(resets on MyService.OnStop())

Class FSMonitor
private MyWatcher as FileSystemWatcher
Public Sub New()
MyWatcher = New FileSystemWatcher
With MyWatcher
.Path = "c:\MyFolder"
.Filter = ".xml"
.IncludeSubDirectories = False
.NotifyFilter = NotifyFilters.CreationTime Or
NotifyFilters.LastWrite OR etc..

AddHandler .Created AddressOf OnChanged()
End With

Private Sub OnChanged(byval obj as object, byval e as
FileSystemEventArgs)
If e.ChangeType = WatcherChangeTypes.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 5402
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(2000)
Loop

Cheers
Ali

"Megan" <an*******@discussions.microsoft.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 Filesystemwatcher 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.OnStart() sets the FSW's EnableRaisingEvents.
(resets on MyService.OnStop())

Class FSMonitor
private MyWatcher as FileSystemWatcher
Public Sub New()
MyWatcher = New FileSystemWatcher
With MyWatcher
.Path = "c:\MyFolder"
.Filter = ".xml"
.IncludeSubDirectories = False
.NotifyFilter = NotifyFilters.CreationTime Or
NotifyFilters.LastWrite OR etc..

AddHandler .Created AddressOf OnChanged()
End With

Private Sub OnChanged(byval obj as object, byval e as
FileSystemEventArgs)
If e.ChangeType = WatcherChangeTypes.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
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...
0
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...
1
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...
2
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...
3
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 "...
20
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...
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. ...
1
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...
5
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,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
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
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...

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.