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

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 15573
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: BK Kim | last post by:
Hello. I am trying to make an application that monitors a specific folder. Once there is a new file copied, it needs to execute some methods. However, I realized that Created event in...
2
by: Phil396 | last post by:
I have a Windows service that use a FileSystemWatcher to scan incoming html for information. The process worked fine in a Windows Application and it also works fine in a Windows Service except...
5
by: Russell | last post by:
Hi all, I have a web page(a) that has a link to another web page(b). Now, on the page load event of web page(b), I am doing the following: Response.redirect("./test.pdf") This pdf, if the...
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...
0
by: user | last post by:
I've recently read that the FileSystemWatcher in vb.net doesn *not* work when watching a mounted linux server's driver (via samba, in this case) because the drive must be NTFS in order to be...
4
by: mammen | last post by:
Hello, I'm writing trace log of various actions happening in my ASP.NET web application by opening a text file when the user logs in to the system and closing the file while the user logs out....
2
by: VI | last post by:
Hi all, I have a problem with the fsw object, and maybe someone experienced with the fsw can help me out. First I want to a share a snippet of the code with you: private void...
2
by: piola vago | last post by:
I'm making an application that monitor a particular file (not a directoy) and when trigger a change event it start a backup process of the changed file. I'm using FileSystemWatcher component. My...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.