Move a file that isn't completely written yet.

T

tshad

I have a filewatcher program that tells me that a file has been created in
my folder. The problem I want to immediately move the file or access it.
If the file is large, it not be completely copied yet.

How do I tell FileWatcher to only tell me when the file is completely there?

fileWatcher = new FileSystemWatcher();
fileWatcher.Path = SemaSettings.InputFilePath;
fileWatcher.NotifyFilter = NotifyFilters.DirectoryName |
NotifyFilters.FileName;
fileWatcher.Filter = "*.*";
fileWatcher.Created += new FileSystemEventHandler(OnChanged);
fileWatcher.EnableRaisingEvents = true;

Thanks,

Tom
 
T

tshad

Nicholas Paldino said:
Tom,

This question is being asked a great deal lately. Apparently file-drop
programs are all the rage now:

http://groups.google.com/group/micr...stemwatcher+nicholas+Paldino#e78b58698181295c
That is what I was afraid of.

I don't have anyway to control how the file is sent. I did try:

FileInfo t = new FileInfo(e.FullPath);
t.attributes;

Hoping that the file would show the attribute "offline" while it was being
written, but that wasn't the case. The only thing it showed was "archive".

The only way I have been able to get it to work is to do a:

System.Threading.Thread.Sleep(2000);
Just to make sure. Not the best way, but will do the job for small files.

Thanks,

Tom
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

tshad said:
I have a filewatcher program that tells me that a file has been created in
my folder. The problem I want to immediately move the file or access it.
If the file is large, it not be completely copied yet.

How do I tell FileWatcher to only tell me when the file is completely
there?

fileWatcher = new FileSystemWatcher();
fileWatcher.Path = SemaSettings.InputFilePath;
fileWatcher.NotifyFilter = NotifyFilters.DirectoryName |
NotifyFilters.FileName;
fileWatcher.Filter = "*.*";
fileWatcher.Created += new FileSystemEventHandler(OnChanged);
fileWatcher.EnableRaisingEvents = true;

Thanks,

Tom
 
N

Nicholas Paldino [.NET/C# MVP]

You really will have to cycle in a loop until you are able to move the
file, as it might take more than 2 seconds to write the file.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

tshad said:
Nicholas Paldino said:
Tom,

This question is being asked a great deal lately. Apparently
file-drop programs are all the rage now:

http://groups.google.com/group/micr...stemwatcher+nicholas+Paldino#e78b58698181295c
That is what I was afraid of.

I don't have anyway to control how the file is sent. I did try:

FileInfo t = new FileInfo(e.FullPath);
t.attributes;

Hoping that the file would show the attribute "offline" while it was being
written, but that wasn't the case. The only thing it showed was
"archive".

The only way I have been able to get it to work is to do a:

System.Threading.Thread.Sleep(2000);
Just to make sure. Not the best way, but will do the job for small files.

Thanks,

Tom
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

tshad said:
I have a filewatcher program that tells me that a file has been created
in my folder. The problem I want to immediately move the file or access
it. If the file is large, it not be completely copied yet.

How do I tell FileWatcher to only tell me when the file is completely
there?

fileWatcher = new FileSystemWatcher();
fileWatcher.Path = SemaSettings.InputFilePath;
fileWatcher.NotifyFilter = NotifyFilters.DirectoryName |
NotifyFilters.FileName;
fileWatcher.Filter = "*.*";
fileWatcher.Created += new FileSystemEventHandler(OnChanged);
fileWatcher.EnableRaisingEvents = true;

Thanks,

Tom
 
T

tshad

Nicholas Paldino said:
You really will have to cycle in a loop until you are able to move the
file, as it might take more than 2 seconds to write the file.

You're right.

But what I do from a piece of code I found was to look at reading the file,
and if I get an error, I wait x number of seconds and try again until I
reach some max number of seconds.

It works real well.

Thanks,

Tom
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

tshad said:
Nicholas Paldino said:
Tom,

This question is being asked a great deal lately. Apparently
file-drop programs are all the rage now:

http://groups.google.com/group/micr...stemwatcher+nicholas+Paldino#e78b58698181295c
That is what I was afraid of.

I don't have anyway to control how the file is sent. I did try:

FileInfo t = new FileInfo(e.FullPath);
t.attributes;

Hoping that the file would show the attribute "offline" while it was
being written, but that wasn't the case. The only thing it showed was
"archive".

The only way I have been able to get it to work is to do a:

System.Threading.Thread.Sleep(2000);
Just to make sure. Not the best way, but will do the job for small
files.

Thanks,

Tom
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I have a filewatcher program that tells me that a file has been created
in my folder. The problem I want to immediately move the file or access
it. If the file is large, it not be completely copied yet.

How do I tell FileWatcher to only tell me when the file is completely
there?

fileWatcher = new FileSystemWatcher();
fileWatcher.Path = SemaSettings.InputFilePath;
fileWatcher.NotifyFilter = NotifyFilters.DirectoryName |
NotifyFilters.FileName;
fileWatcher.Filter = "*.*";
fileWatcher.Created += new FileSystemEventHandler(OnChanged);
fileWatcher.EnableRaisingEvents = true;

Thanks,

Tom
 

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

Top