C# 指定日期时间的FileSystemWatcher?
我想指定FileSystemWatcher来检查系统中的文件自datetime以来是否已更改:C# 指定日期时间的FileSystemWatcher?,c#,C#,我想指定FileSystemWatcher来检查系统中的文件自datetime以来是否已更改: FileSystemWatcher watcher = new FileSystemWatcher(); watcher.Path = @"C:\MyDirectory"; watcher.Changed+= new RenamedEventHandler(watcher_Changed) watcher.EnableRaisingEvents = true; 如何指定它:当启动观察程序时,文件夹
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"C:\MyDirectory";
watcher.Changed+= new RenamedEventHandler(watcher_Changed)
watcher.EnableRaisingEvents = true;
如何指定它:当启动观察程序时,文件夹中已经存在文件。如果文件在特定日期和时间之后,fileSystemWatcher将收到通知。如何使用FileSystemWatcher实现它?如果没有,还有什么其他选择
提前谢谢 下面是一段您可以尝试的内容。。您还可以查看此处的链接以及MSDN中的代码示例 我发现它:
在使用watcher.Notify查看文件夹之前,我必须先检查文件夹中的所有文件,如果它们是新的,然后再进行处理。您可以在此处找到详细信息:devn我添加了一个您正在查找的丢失事件的示例,以及3个您可以检查的其他事件,如果您愿意检查这些事件,请全部检查。我正在寻找这样的东西:NotifyFilters.CreationTime.CompareTo(DateTime.Parse(lastDateTime))>0;但它似乎不起作用。我想检查文件夹中的现有文件,如下所示well@devn-您必须手动检查现有文件,因为它们不会触发
FileSystemWatcher
事件。您可以使用Directory.EnumerateFiles
来处理与注释中的NotifyFilters代码类似的Where
过滤器。FileSystemWatcher不会检查现有文件。您必须手动执行此操作。
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = "*.txt";
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching.
watcher.EnableRaisingEvents = true;