是否有可能實施FileSystemWatcher的委託方法?如何在FileSystemWatcher中實現委託?
我有一個名爲myDelegate的委託方法,我想在定時器函數OnElapsedTimer中調用,我試着調用myDelegate來調用OnDeleted,但第二個參數給出了一個編譯錯誤。我如何在委託方法中傳遞FileSystemEventArgs?
using System;
using System.IO;
using System.Security.Permissions;
using System.Timers;
public class Watcher
{
public delegate void myDelegae(object source, FileSystemEventArgs e);
public static void Main()
{
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnElapsedTimer);
aTimer.Interval = 5000;
aTimer.Enabled = true;
Run();
while (Console.Read() != 'q') ;
}
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void Run()
{
string path = @"C:\\File";
FileSystemWatcher watcher = new FileSystemWatcher(path);
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Attributes;
watcher.NotifyFilter = NotifyFilters.DirectoryName;
// Add event handlers.
watcher.Deleted += new FileSystemEventHandler(OnDeleted);
// Begin watching.
watcher.EnableRaisingEvents = true;
}
private static void OnDeleted(object source, FileSystemEventArgs e)
{
Console.WriteLine("File: {0}", e.FullPath);
}
private static void OnElapsedTimer(object source, ElapsedEventArgs e)
{
Console.WriteLine("Hello World!");
if (!Directory.Exists("C:\\File"))
{
//Invoke OnDeleted here
var fseArgs = new FileSystemEventArgs(WatcherChangeTypes.Deleted, @"C:\", @"File");
OnDeleted("C:\\File",fseArgs);
}
}
}
請張貼編譯錯誤的文字...這將有助於。感謝 – Timmerz 2012-03-29 18:33:19
修改後的代碼,似乎現在的工作我 – kanden 2012-03-29 18:58:44