我正在使用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);
}
谢谢!