FileSystemWatcher 检测 zip 文件
FileSystemWatcher detection of zip files
我正在使用 FileSystemWatcher
class 来检测目录中创建的新文件。我正在创建 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 挑剔?
编辑:添加代码块
我正在使用 FileSystemWatcher
class 来检测目录中创建的新文件。我正在创建 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 挑剔?
编辑:添加代码块