Search code examples
c#-4.0filesystemwatcher

Filesystem Watcher OnDelete Event fires


I have a FileSystemWatcher that monitors a folder for zip files and then performs an extraction of the files contents when added to the folder. All works well when copying or moving the files to the folder as expected. Chrome or IE browsers Save Target As or Save Link As hyperlinks download the file to the folder but the OnCreated event does not fire. Instead it fires OnDeleted when saving the zip file to the folder this way. The download hyperlink could be either web server based or local file to pc, neither which work. Is there a separate notify-filter that needs to be applied for this?

    protected override void OnLoad(EventArgs e)
    {
        Visible = false; // Hide form window.
        ShowInTaskbar = false; // Remove from taskbar.

        if (!Directory.Exists(@"c:\fwbuilder"))
        {
            Directory.CreateDirectory(@"c:\fwbuilder");
        }

        if (!Directory.Exists(@"c:\fwbuilder\romdata"))
        {
            Directory.CreateDirectory(@"c:\fwbuilder\romdata");
        }

        // Create a new FileSystemWatcher object.
        FileSystemWatcher fsWatcher = new FileSystemWatcher();

        //listBox.Items.Add("Started FileSystemWatcher Service…");
        fsWatcher.Path = @"c:\fwbuilder";
        // Set Filter.
        fsWatcher.Filter = "*.zip";
        // Monitor files and subdirectories.
        fsWatcher.IncludeSubdirectories = false;
        // Monitor all changes specified in the NotifyFilters.
        fsWatcher.NotifyFilter = NotifyFilters.Attributes |
                                 NotifyFilters.CreationTime |
                                 NotifyFilters.DirectoryName |
                                 NotifyFilters.FileName |
                                 NotifyFilters.LastAccess |
                                 NotifyFilters.LastWrite |
                                 NotifyFilters.Security |
                                 NotifyFilters.Size;
        fsWatcher.EnableRaisingEvents = true;
        // Raise Event handlers.
        fsWatcher.Changed += new FileSystemEventHandler(OnChanged);
        fsWatcher.Created += new FileSystemEventHandler(OnCreated);
        fsWatcher.Deleted += new FileSystemEventHandler(OnDeleted);
        fsWatcher.Renamed += new RenamedEventHandler(OnRenamed);
        fsWatcher.Error += new ErrorEventHandler(OnError);

        base.OnLoad(e);
    }

Solution

  • It's difficult to tell what exactly is happening on the disk without seeing full details of file changes.

    I suggest you do the following small experiment to better understand what happens when you download a file from browser and save it to the folder being watched by your program.

    1. Remove *.zip filter from your watcher and watch all files
    2. Write a log entry from all your event handlers along with timestamp and file name.
    3. Run the program and save a file and observe the log.

    It is likely that the browser is creating a temp file and renaming it to the desired name at the end of download. If this is indeed the case, you can start watching the rename operation and trigger your extract operation in there.

    I guess you are missing the create event on the temp file because of .zip filter.

    Try this and let me know how it goes. Also, post the result of the experiment (the log).