我需要將文件的創建或複製/移動事件記錄到我將使用的文件夾中,我將使用FileSystemWatcher
。問題是,當我在文件夾中粘貼一個文件時,FileSystemWatcher
將引發一個創建事件。因此,如果我在該文件夾中共同粘貼10
文件,則FileSystemWatcher會引發10個事件。我的要求是如果同時複製文件夾中的所有10個文件,則只會引發一個事件。一次引發FileSystemWatcher的多個事件
請建議。以下是我使用MSDN教程編寫的代碼。
公共類FileSystemWatcherUtil2 {
public static void Main()
{
Run();
}
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void Run()
{
/* creation of a new FileSystemWatcher and set its properties */
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"C:\Users\TestFolder";
/*watch for internal folder changes also*/
watcher.IncludeSubdirectories = true;
/* Watch for changes in LastAccess and LastWrite times, and the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
/* event handlers */
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
/* watching started */
watcher.EnableRaisingEvents = true;
/* user should quit the program to stop watching*/
Console.WriteLine("Press \'q\' to quit the sample.");
while (Console.Read() != 'q') ;
}
/* event handlers definition for changed and renamed */
private static void OnChanged(object source, FileSystemEventArgs e)
{
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
}
我認爲唯一的真正答案OP的是,你不能,10個文件創建10個事件。這是一個FileSystemWatcher的,而不是一個PasteWatcher;) – Lazarus 2012-02-21 11:13:19
@Lazarus:您可以使用包裝這將提高每10個內部'Created'事件OCCURENCES – sll 2012-02-21 11:27:15
沒有自己的'CreatedEx'有一次,還是依靠10個內部'Created'事件存在的在彼此的超時期限內(在你的例子中是200ms)?您是否確實能夠確定這10個事件是單個粘貼活動的結果,而不是批處理文件複製文件的結果? – Lazarus 2012-02-21 11:35:30