469,270 Members | 1,011 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

Filesystemwatcher detecting File Closing. How ?

I have a program that uses the filesystemwatcher.

Problem is that i have to wait until the file is closed before i can do
someting with it in the changed event.

How can i detect if the foreighn application has finished writing and has
closed the file ?
Johan

Nov 16 '05 #1
3 14469
filesystemwatcher can detect if a file has changed, appeared, etc. but you
would likely need to use another method to determine if the file is
"closed".

The first thing that comes to mind is when a new file is detected, to
repeatedly poll to attempt to read from the file and handle the result (nice
if you can do so without using an exception for business logic). Once you
can read from the file, you have a high degree (not 100%) of assurance that
its finished being written.

Maybe somebody else can come up with a more elegant idea.
Peter

"Sagaert Johan" <RE*************@hotmail.com> wrote in message
news:u5**************@TK2MSFTNGP12.phx.gbl...
I have a program that uses the filesystemwatcher.

Problem is that i have to wait until the file is closed before i can do
someting with it in the changed event.

How can i detect if the foreighn application has finished writing and has
closed the file ?
Johan

Nov 16 '05 #2
I have tackled this exact issue, and there is no built-in way to detect
when the file is free of locks by other processes. Who knows, some
user may have a Word document open and locked in that folder, and your
code needs to be able to handle this situation. Ok, I know that
probably won't happen, but another process has the file locked if it's
still being written. My solution was to listen for FileName and
LastWrite events. I would then attempt to access the file. If it is
locked, you'll just have a try again later. Ahh, but you won't get
another FileSystemWatcher event for that file, so you'll just have to
be innovative. Yes, you can poll and swallow the exceptions that occur
if the file is locked, but be careful that your program doesn't hang if
the file doesn't become unlocked (or if the file is very large).

If only a few files come through, you may want to sent a delegate to
the threadpool to sleep for a second and try to process the file again.
.. . and not give up until it is successful. This way, your main
program can continue to function.

The bottom line is that you will have to be innovative about how you
solve this problem because the solution will have to be unique to your
scenario.

Best regards,
Jeffrey Palermo
Blog: http://www.jeffreypalermo.com

Nov 16 '05 #3
Solved :

i use
private void fileSystemWatcher1_Changed(object sender,
System.IO.FileSystemEventArgs e)

{

FileStream fs=null;

try

{

fs=new
FileStream(e.FullPath,FileMode.Open,FileAccess.Rea dWrite,FileShare.None,100)
;

fs.Close();

Console.WriteLine("closed, ready to process the file ");

}

catch

{

}

}
"Peter Bromberg [C# MVP]" <pb*******@yahoo.com> wrote in message
news:uc**************@TK2MSFTNGP15.phx.gbl...
filesystemwatcher can detect if a file has changed, appeared, etc. but you
would likely need to use another method to determine if the file is
"closed".

The first thing that comes to mind is when a new file is detected, to
repeatedly poll to attempt to read from the file and handle the result (nice if you can do so without using an exception for business logic). Once you
can read from the file, you have a high degree (not 100%) of assurance that its finished being written.

Maybe somebody else can come up with a more elegant idea.
Peter

"Sagaert Johan" <RE*************@hotmail.com> wrote in message
news:u5**************@TK2MSFTNGP12.phx.gbl...
I have a program that uses the filesystemwatcher.

Problem is that i have to wait until the file is closed before i can do
someting with it in the changed event.

How can i detect if the foreighn application has finished writing and has closed the file ?
Johan


Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Phil396 | last post: by
5 posts views Thread by Russell | last post: by
reply views Thread by user | last post: by
2 posts views Thread by VI | last post: by
2 posts views Thread by piola vago | last post: by
5 posts views Thread by =?Utf-8?B?Sm9obiBT?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.