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

FileSystemWatcher

Hi,
I have a little application that watches a FTP folder. When a file is
uploaded, it takes that file and moves it to another folder on the same
server.
Here are a couple of lines from my C# app:

FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Created += new FileSystemEventHandler(OnChanged);

My problem is that my folder watcher does not know when the file is finish
uploading. So if the file is 3mb my file watcher moves the file before it is
finished uploading, and the resulting new file in the new folder is only like
24k.

How can I "KNOW" when the file is finish uploading? Is there some type of
windows event I can watch?

Thanks,
Michael

Aug 24 '06 #1
4 7201
>
How can I "KNOW" when the file is finish uploading? Is there some type of
windows event I can watch?
I am not sure if this is an optimal solution, but if you can get a write
lock on the file, nobody else is having a write lock on it. (just don't
overwrite it)

Try to open the file in 'append' mode or 'write' mode, and check the result.

/Søren
Aug 24 '06 #2

Michael wrote:
Hi,
I have a little application that watches a FTP folder. When a file is
uploaded, it takes that file and moves it to another folder on the same
server.
Here are a couple of lines from my C# app:

FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Created += new FileSystemEventHandler(OnChanged);

My problem is that my folder watcher does not know when the file is finish
uploading. So if the file is 3mb my file watcher moves the file before it is
finished uploading, and the resulting new file in the new folder is only like
24k.

How can I "KNOW" when the file is finish uploading? Is there some type of
windows event I can watch?

Thanks,
Michael
I found this and thought it might help you:

http://www.jasonstorch.com/

"If you have read my previous post on the FileSystemWatcher control, I
posted code for a simple service that runs in the background ad copies
files from one server to another. Basically, the fix is to add the
following few lines of code to the create and change events:

while (!File.Exists(sample.file))
{
//keep looping until the file is unlocked.
}"

The page has other code as well. Hope this helps.

Aug 24 '06 #3
Hi,
couldn't this cause an endless loop?
Michael

"ne***********@gmail.com" wrote:
>
Michael wrote:
Hi,
I have a little application that watches a FTP folder. When a file is
uploaded, it takes that file and moves it to another folder on the same
server.
Here are a couple of lines from my C# app:

FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Created += new FileSystemEventHandler(OnChanged);

My problem is that my folder watcher does not know when the file is finish
uploading. So if the file is 3mb my file watcher moves the file before it is
finished uploading, and the resulting new file in the new folder is only like
24k.

How can I "KNOW" when the file is finish uploading? Is there some type of
windows event I can watch?

Thanks,
Michael

I found this and thought it might help you:

http://www.jasonstorch.com/

"If you have read my previous post on the FileSystemWatcher control, I
posted code for a simple service that runs in the background ad copies
files from one server to another. Basically, the fix is to add the
following few lines of code to the create and change events:

while (!File.Exists(sample.file))
{
//keep looping until the file is unlocked.
}"

The page has other code as well. Hope this helps.

Aug 24 '06 #4
I would use a System.Threading.Timer delegate (after the dir changed event)
to kick off every minuite or so to check the "CanWrite" status or catch the
open exception. This assumes the FTP process has opened the file with
exclusive write access, which it should. If it incorrectly opens the file
with shared read/write, then another solution may be needed.

--
William Stacey [MVP]

"Michael" <Mi*****@discussions.microsoft.comwrote in message
news:C8**********************************@microsof t.com...
| Hi,
| couldn't this cause an endless loop?
| Michael
|
| "ne***********@gmail.com" wrote:
|
| >
| Michael wrote:
| Hi,
| I have a little application that watches a FTP folder. When a file is
| uploaded, it takes that file and moves it to another folder on the
same
| server.
| Here are a couple of lines from my C# app:
|
| FileSystemWatcher watcher = new FileSystemWatcher();
| watcher.Created += new FileSystemEventHandler(OnChanged);
|
| My problem is that my folder watcher does not know when the file is
finish
| uploading. So if the file is 3mb my file watcher moves the file
before it is
| finished uploading, and the resulting new file in the new folder is
only like
| 24k.
|
| How can I "KNOW" when the file is finish uploading? Is there some
type of
| windows event I can watch?
|
| Thanks,
| Michael
| >
| I found this and thought it might help you:
| >
| http://www.jasonstorch.com/
| >
| "If you have read my previous post on the FileSystemWatcher control, I
| posted code for a simple service that runs in the background ad copies
| files from one server to another. Basically, the fix is to add the
| following few lines of code to the create and change events:
| >
| while (!File.Exists(sample.file))
| {
| //keep looping until the file is unlocked.
| }"
| >
| The page has other code as well. Hope this helps.
| >
| >
Aug 24 '06 #5

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

Similar topics

4
by: Josh Usovsky | last post by:
I'm setting up a watched folder using FileSystemWatcher. When I drop a small file into the watched folder, I can respond to a .Created event and process the file with other code. However, if I try...
1
by: Troy Murphy | last post by:
How do I prevent the FileSystemWatcher event to keep firing while the file is being created? When copying a file to the watched folder, the event fires a dozen or more times! Also, the...
7
by: Allen Anderson | last post by:
I'm trying to figure out a way to catch when a file has been written to a directory. I currently have it where I can catch when the file begins writing, but this isn't helpful as I need to know...
2
by: Jet Leung | last post by:
Hi all, I had made a program to watching files in my directory. I had used a instance of FileSystemWatcher to do my work.And I had add some events of the FileSystemWatcher , for example onChange,...
13
by: David | last post by:
I have been working on trying to write a directory watcher service. One of the requirments is that it be able to watch multiple directories, not sub directories of one parent directory, but just...
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?
5
by: Goran Djuranovic | last post by:
Hi all, I have a file system watcher service that works fine on a local hard drive, but will not work across the network. I tried both: mapping the drive and "\\..." path both no luck. I don't...
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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
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: 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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.