我使用FileSystemWatcher
類來檢測在目錄中創建的新文件。我正在創建txt和zip文件。它檢測完美的txt文件,但與zip文件不一樣。我知道是否有人曾與此和你的經驗。FileSystemWatcher檢測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);
}
謝謝!
這可能是該文件與另一個延伸,而在另一個目錄,然後移動或在您的choosen路更名爲最初創建。您使用哪個應用程序來創建zip文件? – Steve
你怎麼稱呼你的'CreateWatcher'方法?因爲它看起來像實際的'FileSystemWatcher'對象立即超出範圍。 –
要創建zip文件我正在使用一個簡單的控制檯應用程序,它使用donetzip庫來創建它。 –