views:

84

answers:

4

How to determine when file copying is ended i'm using c#

edit: we copying files through network from one pc to another one. my task is to watch directory and do some actions after files are copied to it.

+2  A: 

Periodically check for size. Anyway, I recommend you to use CopyFileEx, that has a progress feature (http://msdn.microsoft.com/en-us/library/aa363852(VS.85).aspx)

thelost
i thought about this idea before. when i should stop checking.
when the size is equal to the size of the original file.
thelost
i should tell you guys before that i'm copying file from one pc to another through network. i'm copying file not from code.
even so, you could get the size before starting to copy.
thelost
how i can get it? filesystem watcher doesn't provide this information
it should not count whether the file is on your disk or on some other computer in your network - it's only the address that differs.
thelost
+3  A: 

If you are using File.Copy() then this operation is finished after file is actually copied.

Andrew Bezzub
i'm copying files not from code.
+1  A: 

I have came across something similar recently. I would use File's open write attribute on the file to see if you can write to the file: e.g. FileStream fs = f.OpenWrite();

if the above statement works then file is not in use i.e. done copying.

activebiz
well, can i somehow manipulate with copying file dialog on pc (to determine when it is closed)?i.e. not using watcher on target dir
I dont know if this is a best thing to do. To start with you will need to identify process . The file copying process comes under "windows explorer" (if you use task manager). Unless someone knows how to get the correct process easily.One other way to do is to try to rename the destination directory (e.g. something like Directory.Move()) if you can do this then directory is not in use and all files have been copied to the directory.thx
activebiz
+3  A: 

Try looking into FileSystemWatcher's Created event.

Some common occurrences, such as copying or moving a file or directory, do not correspond directly to an event, but these occurrences do cause events to be raised. When you copy a file or directory, the system raises a Created event in the directory to which the file was copied

KMan
The event will be raised as soon as the file is created. It does not mean that data in file in completely written.
cornerback84