2

我正在使用FileSystemWatcher类来检测在目录中创建的新文件。我正在创建 txt 和 zip 文件。它可以检测到完美的 txt 文件,但与 zip 文件不同。我知道是否有人与您合作过以及您的经验。

这是我的代码:

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);
    } 

谢谢!

4

1 回答 1

2

*.*如果您将过滤器更改为然后将 zip 文件放入目录,它会做任何事情吗?与 txt 文件相比,zip 文件是否很大?您是否尝试过 FileSystemWatcher 挑剔,文件名区分大小写?

编辑:添加代码块

于 2015-11-12T16:35:03.200 回答