views:

211

answers:

2

I have a simple app using FileSystemWatcher running as a Windows Service. Files are saved to the directory via an excel VB Macro with

ActiveWorkbook.SaveAs Filename:= "pathToSaveTo"

On creation of a new file, the watcher calls a method to process the file

void watcher_FileCreated(object sender, FileSystemEventArgs e)
    {
        while (true)
        {
            if (FileUploadComplete(e.FullPath))
            {
                this.ProcessOneFile(e.FullPath, e.Name);
                break;
            }
        }
    }

The watcher app never registers an event when this happens but manually removing and re-adding the files to folder causes the event to be raised.

Does anybody know how I can get the expected behaviour when a file is saved to the directory?

+2  A: 

Just a shot in the dark, perhaps the implementation of 'SaveAs' is doing something oddball like writing the data to a temp file and then 'moving' it into the final directory, which might fire as a 'Renamed' or 'Changed' Event instead.

If you haven't already done so, try registering for these other events and see if they fire on a 'SaveAs' perhaps?

JStriedl
Yep, that was it! Adding watcher.Renamed += new RenamedEventHandler(watcher_FileRenamed);did the trick. Thanks for quick replies.
friedX
A: 

If the file already exists and is being overwritten, you won't get a Created event. Try hooking up the Changed event as well.

dangph