【发布时间】:2018-09-26 20:26:26
【问题描述】:
我想跟踪特定路径的文件更改,我已经完成了现在工作正常的代码。它正在跟踪文件创建、重命名和更改。
我的问题是当我启动 Filesystemwatcher 时它工作正常,但一段时间后它停止工作,即它停止触发创建、删除和更改事件。
谁能帮帮我?
提前谢谢你。
这是我的代码 lstFolder 是我的多路径列表
this.listFileSystemWatcher = new List();
// Loop the list to process each of the folder specifications found
if (lstFolder.Count > 0)// check if path is available to watch else exit file watcher
{
foreach (CustomFolderSettings customFolder in lstFolder)
{
DirectoryInfo dir = new DirectoryInfo(customFolder.FWPath);
// Checks whether the folder is enabled and
// also the directory is a valid location
if (dir.Exists)//customFolder.FolderEnabled &&
{
customFolder.AllowedFiles = customFolder.FWExtension;// setting extension to allowed filw extension to log .
foreach (var strExt in customFolder.FWExtension.Split(','))
{
// Creates a new instance of FileSystemWatcher
//FileSystemWatcher fileSWatch = new FileSystemWatcher();
this.fileSWatch = new FileSystemWatcher();
// Sets the filter
fileSWatch.Filter = strExt;// customFolder.FolderFilter;
// Sets the folder location
fileSWatch.Path = customFolder.FWPath;
fileSWatch.InternalBufferSize = 64000;
// Sets the action to be executed
StringBuilder actionToExecute = new StringBuilder(customFolder.ExecutableFile);
// List of arguments
StringBuilder actionArguments = new StringBuilder(customFolder.ExecutableArguments);
// Subscribe to notify filters
fileSWatch.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Associate the events that will be triggered when a new file Created,Changed,Deleted,Renamed //
// is added to the monitored folder, using a lambda expression
fileSWatch.Created += (senderObj, fileSysArgs) => fileSWatch_Created(senderObj, fileSysArgs, actionToExecute.ToString(), customFolder.AllowedFiles);
fileSWatch.Changed += (senderObj, fileSysArgs) => fileSWatch_Changed(senderObj, fileSysArgs, actionToExecute.ToString(), customFolder.AllowedFiles);
fileSWatch.Deleted += (senderObj, fileSysArgs) => fileSWatch_Deleted(senderObj, fileSysArgs, actionToExecute.ToString(), customFolder.AllowedFiles);
fileSWatch.Renamed += (senderObj, fileSysArgs) => fileSWatch_Renamed(senderObj, fileSysArgs, actionToExecute.ToString(), customFolder.AllowedFiles);
fileSWatch.Error += (senderObj, fileSysArgs) => fileSWatch_Error(senderObj, fileSysArgs, actionToExecute.ToString(), customFolder.AllowedFiles);
// will track changes in sub-folders as well
fileSWatch.IncludeSubdirectories = customFolder.FWSubFolders;
// Begin watching
fileSWatch.EnableRaisingEvents = true;
// Add the systemWatcher to the list
listFileSystemWatcher.Add(fileSWatch);
GC.KeepAlive(fileSWatch);
GC.KeepAlive(listFileSystemWatcher);
}
}
}
}
else
{
Application.Exit();
}
【问题讨论】:
-
它是完全停止工作还是丢弃事件?
-
我无法理解。经过一段时间的空闲时间后,它无法跟踪文件更改。
-
我想我在几年前读过一些类似的东西。问题被设置为一个非常大的缓冲区。我看到你使用
fileSWatch.InternalBufferSize。尝试更改该值,或将其删除。我不知道很久以前的问题是什么,但我认为速度很慢,因为缓冲区大(不幸的是我不记得它是否也有停止事件的问题)。 -
(萨鲁曼回应后) 当你的文件改动太多,无法使用指定过滤时,尝试返工函数
fileSWatch_*只收集事件(例如到List)并在较低优先级的线程中处理信息。这至少应该允许您以更快的速度清除缓冲区。但请记住,长缓冲区意味着处理速度较慢(在系统中)。
标签: c#