我有一个 Windows 服务(并通过创建类似的 WinForms 应用程序验证了代码),其中 NotifyFilter 不起作用。只要我删除那行代码,服务就可以正常工作,我可以在 WinForms 应用程序中看到事件处理程序触发。
我所做的只是将一个文本文件放到输入目录中,以便 FileSystemWatcher 启动watcher_FileChanged
委托。当我_watcher.NotifyFilter = NotifyFilters.CreationTime;
在那里时,它不起作用。当我把它拉出来时,它工作正常。
谁能告诉我这个过滤器是否有问题?
这是 OnStart 事件的 FSW 代码。
protected override void OnStart(string[] args)
{
_watcher = new FileSystemWatcher(@"C:\Projects\Data\Test1");
_watcher.Created += new FileSystemEventHandler(watcher_FileChanged);
_watcher.NotifyFilter = NotifyFilters.CreationTime;
_watcher.IncludeSubdirectories = false;
_watcher.EnableRaisingEvents = true;
_watcher.Error += new ErrorEventHandler(OnError);
}
private void watcher_FileChanged(object sender, FileSystemEventArgs e)
{
// Folder with new files - one or more files
string folder = @"C:\Projects\Data\Test1";
System.Console.WriteLine(@"C:\Projects\Data\Test1");
//Console.ReadKey(true);
// Folder to delete old files - one or more files
string output = @"C:\Temp\Test1\";
System.Console.WriteLine(@"C:\Temp\Test1\");
//Console.ReadKey(true);
// Create name to call new zip file by date
string outputFilename = Path.Combine(output, string.Format("Archive{0}.zip", DateTime.Now.ToString("MMddyyyy")));
System.Console.WriteLine(outputFilename);
//Console.ReadKey(true);
// Save new files into a zip file
using (ZipFile zip = new ZipFile())
{
// Add all files in directory
foreach (var file in Directory.GetFiles(folder))
{
zip.AddFile(file);
}
// Save to output filename
zip.Save(outputFilename);
}
DirectoryInfo source = new DirectoryInfo(output);
// Get info of each file into the output directory to see whether or not to delete
foreach (FileInfo fi in source.GetFiles())
{
if (fi.CreationTime < DateTime.Now.AddDays(-1))
fi.Delete();
}
}