我正在尝试实施一个 Filewatcher ,它会为已创建或已更改(如果数据已更改)观看目录 FilesDir 文件。如果我只复制粘贴 FileDir 中的一个文件,它可以正常工作,但如果我复制粘贴多个文件,则只调用其中一个 File_Created 事件文件
public void SetUpFileWatcher()
{
var sourcePath = ConfigurationManager.AppSettings["DirectoryPath"];
var fileSystemWatcher = new FileSystemWatcher();
fileSystemWatcher.IncludeSubdirectories = false;
fileSystemWatcher.Filter = ConfigurationManager.AppSettings["FileType"];
fileSystemWatcher.Path = sourcePath;
fileSystemWatcher.EnableRaisingEvents = true;
fileSystemWatcher.Created += FileMonitorWatcher_FileCreated;
fileSystemWatcher.Changed += FileMonitorWatcher_FileChanged;
Logger.Info("SetUpFileWatcher", "End of Setting Up File Watcher");
}
/// <summary>
/// Event called when a new file is created.
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">FileSystemEventArgs</param>
private void FileMonitorWatcher_FileCreated(object sender, FileSystemEventArgs e)
{
Logger.Info("FileMonitorWatcher_FileCreated", "File created method started....");
((FileSystemWatcher)(sender)).EnableRaisingEvents = false;
FileMonitorManager fileMonitorMgr = new FileMonitorManager();
fileMonitorMgr.FileCreated(e.FullPath);
((FileSystemWatcher)(sender)).EnableRaisingEvents = true;
Logger.Info("FileMonitorSystemWatcher_Created:End", "Create Function for File Watcher End at: " + DateTime.Now.ToString());
}
/// <summary>
/// Event called when a file is changed.
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">FileSystemEventArgs</param>
private void FileMonitorWatcher_FileChanged(object sender, FileSystemEventArgs e)
{
Logger.Info("FileMonitorWatcher_FileChanged", "Updated method started....");
((FileSystemWatcher)(sender)).EnableRaisingEvents = false;
FileMonitorManager fileMonitorMgr = new FileMonitorManager();
fileMonitorMgr.FileCreated(e.FullPath);
((FileSystemWatcher)(sender)).EnableRaisingEvents = true;
Logger.Info("FileMonitorSystemWatcher_Changed:End", "Update Function for File Watcher End at: " + DateTime.Now.ToString());
}
帮助:
所以,我需要的FileWatcher是只有在创建新文件时才能观看目录并调用 File_Created 事件,而只有在文件中的某些数据时才会调用 File_Changed 事件改变了。
注意:
每当在监视目录中更改或创建文件时,都会为该文件调用 SomeTimeConsumingMethod()方法来填充数据库中的数据。在我的情况下, SomeTimeConsumingMethod()方法大约需要3-4分钟。