我正在使用 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);
}
谢谢!
最佳答案
如果将过滤器更改为 *.*
并将 zip 文件放入此后的目录中,它会执行任何操作吗?与 txt 文件相比,zip 文件是否很大?您是否尝试过 FileSystemWatcher 挑剔文件名区分大小写?
编辑:添加代码块
关于c# - FileSystemWatcher 检测 zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33676365/