FileSystemWatcher

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
Michael said:
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.
 
Hi,
couldn't this cause an endless loop?
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.
 
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]

| Hi,
| couldn't this cause an endless loop?
| Michael
|
| "(e-mail address removed)" 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.
| >
| >
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top