473,386 Members | 1,867 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,386 software developers and data experts.

FileSystemWatcher.. easy

Hello!

I'm using the FileSystemWatcher to get notified when new files are
created in a directory. Once a file appears in the directory I want to
read it's contents into memory and do some processing.

The problem I'm having is that sometimes the FileSystemWatcher event
gets fired before the file is fully written (it gets fired when the file
is created.. not when it is created+closed).

Is there some way to get notified when the new file is completely
written (closed)?

I'm currently starting another thread that I'm passing the file names
to.. it uses a while loop with a Thread.Sleep to test if the file is
complete before it continues with the processing. But that sucks.

thanks,
Max
Nov 17 '05 #1
4 3067
Markus,

Check out this thread:

http://www.dotnet247.com/247referenc...41/207923.aspx

Regards - Octavio

"Markus Stoeger" <sp******@gmx.at> escribió en el mensaje
news:eW**************@TK2MSFTNGP11.phx.gbl...
Hello!

I'm using the FileSystemWatcher to get notified when new files are created
in a directory. Once a file appears in the directory I want to read it's
contents into memory and do some processing.

The problem I'm having is that sometimes the FileSystemWatcher event gets
fired before the file is fully written (it gets fired when the file is
created.. not when it is created+closed).

Is there some way to get notified when the new file is completely written
(closed)?

I'm currently starting another thread that I'm passing the file names to..
it uses a while loop with a Thread.Sleep to test if the file is complete
before it continues with the processing. But that sucks.

thanks,
Max

Nov 17 '05 #2
Octavio Hernandez wrote:
Check out this thread:

http://www.dotnet247.com/247referenc...41/207923.aspx


thanks, that's describing exactly my problem. I'm not sure if I'm
correctly understanding the solution though. As far as I understand,
closing the file will fire the "Changed" event.

Please take a look at the following code. Every time the "Changed" event
gets fired, I'm trying to open the file (with FileShare.None, so I can
be sure I'm the only one who's got the file open). This should only
finish successfully when the file has been closed, shouldn't it?

private void fsw_Changed(object sender, FileSystemEventArgs e) {
System.Diagnostics.Trace.WriteLine("Changed: " + e.Name + ", " +
e.ChangeType);

FileStream fs = tryOpen(e.FullPath);

if (fs != null) {
// do my processing...
fs.Close();
}
}

// return FileStream when open was successful, otherwise null
private FileStream tryOpen(string path) {
FileStream fs = null;

try {
fs = new FileStream(path, FileMode.Open, FileAccess.Read,
FileShare.None);
}
catch (Exception ex) {
if (fs != null) {
fs.Close();
fs = null;
}
}

return fs;
}

thanks,
Max
Nov 17 '05 #3
> thanks, that's describing exactly my problem. I'm not sure if I'm
correctly understanding the solution though. As far as I understand,
closing the file will fire the "Changed" event.
I think you've correctly understood.
Please take a look at the following code. Every time the "Changed" event
gets fired, I'm trying to open the file (with FileShare.None, so I can be
sure I'm the only one who's got the file open).


Seems correctly, carefully programmed AFAICT... What does happen whe you run
this code?

Regards - Octavio
Nov 17 '05 #4
Octavio Hernandez wrote:
thanks, that's describing exactly my problem. I'm not sure if I'm
correctly understanding the solution though. As far as I understand,
closing the file will fire the "Changed" event.


I think you've correctly understood.
Please take a look at the following code. Every time the "Changed" event
gets fired, I'm trying to open the file (with FileShare.None, so I can be
sure I'm the only one who's got the file open).


Seems correctly, carefully programmed AFAICT... What does happen whe you run
this code?


I have implemented it in two of my projects that use the file system
watcher today. It seems to work fine so far. I have additionally set the
NotifyFilter to LastWrite to reduce the number of fired events.

So far it hasn't missed any files yet, but I'm still a bit skeptical
about that. Because if I write lots of data to a file and flush the
stream after every few kB it only occassionally fires the Changed
event.. so it seems like it misses a few. I guess further testing will
show how well it really works.

The only thing I don't like about it is the try/catch around the opening
of the file. Exceptions are slow and ugly and should only be used for...
_exceptions_. Not for testing for things. But I don't see a better way
right now to find out if the file has been closed already.. so I guess
it's ok.

thanks for your help,
Max
Nov 17 '05 #5

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

Similar topics

2
by: Phil Galey | last post by:
I have a FileSystemWatcher that triggers when a PDF file is created. However, the creation of the PDF file is about a 7 or 8 second process ... I cannot refer to the file during that time because...
1
by: Raed Sawalha | last post by:
in windows service I'm using FileSystemWatcher to monitor directory changes , but I have case that by such a service in stopped while stopping time many file were copied to directory , as soon as i...
0
by: Markus Stoeger | last post by:
Hello! I'm using the FileSystemWatcher to get notified when new files are created in a directory. Once a file appears in the directory I want to read it's contents into memory and do some...
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...
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...
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?
3
by: emailtonitin | last post by:
I am facing a design issue in my code related to the filesystemwatcher's filter property. I figured out that the filter won't accept multiple patterns for eg. "*.txt, *.csv" don't work. To...
2
by: naveen.konduri | last post by:
Greetings group Question : Does FileSystemWatcher of System.IO support SAN and NAS? I am designing an assembly which is to record in to DB, when new files are dropped in to a folder, well 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.