2

I'm using FileSystemWatcher class to detect new files created in a directory. I'm creating txt and zip files. It's detects perfectly txt files but not the same with zip file. I know if anyone has worked with that and your experience.

Here is my code:

public void CreateWatcher(String path)
    {
        //Create a new FileSystemWatcher.
        FileSystemWatcher watcher = new FileSystemWatcher(path);

        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                     | NotifyFilters.FileName | NotifyFilters.DirectoryName;

        Thread.Sleep(2000);
        //Set the filter to all type of files
        watcher.Filter = "*.zip";

        //Subscribe to the Created event.
        watcher.Created += new FileSystemEventHandler(watcher_FileCreated);

        //Enable the FileSystemWatcher events.
        watcher.EnableRaisingEvents = true;
    }

    private void watcher_FileCreated(object sender, FileSystemEventArgs e)
    {
        logger.InfoFormat("New zip file created -> " + e.Name);
    } 

Thanks!

  • It is possible that the file is created initially with another extension and in another directory then moved or renamed in your choosen path. Which application do you use to create the zip files? – Steve Nov 12 '15 at 16:33
  • 3
    How do you call your `CreateWatcher` method? Because it looks like the actual `FileSystemWatcher` object immediately goes out of scope. – Peter M Nov 12 '15 at 16:34
  • To create the zip file I'm using a simple console application which uses donetzip library to create it. – Jaime Menendez Llana Nov 12 '15 at 16:34

1 Answers1

2

Does it do anything, if you change the filter to *.* and put the zip file to the directory after that? Is the zip file very large compared to the txt file? Have you tried that is the FileSystemWatcher picky with file names being case-sensitive?

edit: added code block

Oskari3000
  • 131
  • 5
  • Zip file is same large as txt file. The name of the zip file is composed as a autogenerated id (made with `Guid` class and with the now datetime) – Jaime Menendez Llana Nov 12 '15 at 16:38
  • When i put the filter like `*.*`, it shows next: DotNetZip-1zcc020c.tmp for example. – Jaime Menendez Llana Nov 12 '15 at 16:40
  • I was thinking, that would the event fire up if you just manually copy some random zip file to that directory, instead of creating it programmitically? – Oskari3000 Nov 12 '15 at 16:42
  • 1
    OK, it sounds like that it first makes a temp file and after that renames it to zip file. Maybe you need to add some other events to detect the renaming also? – Oskari3000 Nov 12 '15 at 16:43
  • 3
    The problem was I'm renaming the zip file, so there are to launch another event of `FyleSystemWatcher` class `watcher.Renamed += new RenamedEventHandler(OnRenamed);` and making on the event function the action you want to do. Thanks to everybody for the help. – Jaime Menendez Llana Nov 13 '15 at 09:36