我想跟踪特定路径的文件更改,并且我对现在可以正常工作的代码已经完成了很多工作。它正在跟踪文件的创建,重命名和更改。
我的问题是,当我启动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();
}
答案 0 :(得分:2)
不使用
ListView
创建一个GC.KeepAlive(fileSWatch);
GC.KeepAlive(listFileSystemWatcher);
并代替存储每个
也看看
请注意,一些因素会影响哪些文件系统更改事件 如下所述:
- 常见文件系统操作可能引发多个事件。例如,当文件从一个目录移动到另一个目录时, 可能会引发OnChanged以及一些OnCreated和OnDeleted事件。 移动文件是一个复杂的操作,包括多个简单的操作 操作,因此引发多个事件。同样,一些 应用程序(例如,防病毒软件)可能会导致其他 FileSystemWatcher检测到的文件系统事件。
- 只要不切换或删除磁盘,FileSystemWatcher即可监视磁盘。 FileSystemWatcher不会引发以下事件 CD和DVD,因为时间戳和属性无法更改。远程 计算机必须安装了以下必需的平台之一: 组件正常运行。
- 如果多个FileSystemWatcher对象在Service Pack 1或Windows 2000 SP2或更早版本的Windows XP中正在监视同一UNC路径, 那么只有一个对象会引发一个事件。在运行的机器上 Windows XP SP1和更高版本,Windows 2000 SP3或更高版本或Windows Server 2003年,所有FileSystemWatcher对象都会引发相应的事件。
请注意,当缓冲区大小时,FileSystemWatcher可能会丢失事件 被超过。为避免丢失事件,请遵循以下准则:
- 通过设置InternalBufferSize属性来增加缓冲区大小。
- 避免监视长文件名的文件,因为长文件名会导致缓冲区满。考虑重命名这些文件 使用较短的名称。
- 保持事件处理代码尽可能短。
FileSystemWatcher.InternalBufferSize Property
备注
您可以将缓冲区设置为4 KB或更大,但不能超过64 KB KB如果您尝试将InternalBufferSize属性设置为小于 4096个字节,您的值将被丢弃,并且InternalBufferSize 属性设置为4096字节。为了获得最佳性能,请使用多个 在基于Intel的计算机上为4 KB。
系统将文件更改通知组件,并存储这些更改 组件创建的缓冲区中的更改并传递给API。每 事件最多可使用16个字节的内存,不包括文件名。 如果在短时间内有很多更改,缓冲区可能会溢出。 这会导致组件无法跟踪目录中的更改, 而且只会提供一揽子通知。
增加缓冲区的大小可以防止文件系统丢失 更改事件。但是,增加缓冲区大小很昂贵,因为 它来自无法分页到磁盘的非分页内存,因此 保持缓冲区尽可能小。为避免缓冲区溢出,请使用 NotifyFilter和IncludeSubdirectories属性以过滤掉 不需要的更改通知。