C# – Waiting for a copy operation to complete

Yup, use a FileSystemWatcher but instead of watching for the created event, watch for the changed event. After every trigger, try to open the file. Something like this:

var watcher = new FileSystemWatcher(path, filter);
watcher.Changed += (sender, e) => {
    FileStream file = null;
    try {
        Thread.Sleep(100); // hack for timing issues
        file = File.Open(
            e.FullPath,
            FileMode.Open,
            FileAccess.Read,
            FileShare.Read
        );
    }
    catch(IOException) {
        // we couldn't open the file
        // this is probably because the copy operation is not done
        // just swallow the exception
        return;
    }

    // now we have a handle to the file
};

This is about the best that you can do, unfortunately. There is no clean way to know that the file is ready for you to use.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)