Ask the Expert

How can I process a file before it completely copies?

I have a project that is watching a folder for new files. The FileSystemWatcher notifies us when a new file has arrived, but I have to wait until the file has completed its copy before continuing the program. Currently I use a time to wait routine and guess the duration based on its size. Is there any method that can be used to determine when the file has finished copying (which is usually across the LAN)? The OS will let me manipulate the file info (e.g., comparing the files length, name, path and so on), but I have to wait until the file copy is complete to continue processing. Any thoughts?

Requires Free Membership to View

The FileSystemWatcher provides two events you can use in your scenario: Created and Changed. The former is of little use since you need to know when the entire file has finished copying. The later is the one to look for. You will get two calls to that event during a lengthy copy. You get one when the file is created, because the initial attributes of the file are changed (such as the last write attribute). The second call to this event is the one you're interested in, as it's done after copying finished, to set the last modified date and last access time attributes on the file. So you will have to keep a flag so you skip the first event call, and react to the second one.

This was first published in January 2005

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: