是否有可能实施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