- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
场景是我有一个根文件夹来监视任何新文件夹(包含文件)并设置一个计时器来单独压缩每个文件夹。但是,在调用 zip 函数之前,我无法判断文件夹中的文件是否是最后一个文件,因此我想在压缩文件夹之前创建新文件时将计时器重置为该文件夹。
我使用 FileSystemWatcher
来监控根文件夹及其子文件夹。
下面是我尝试的部分代码,源代码可以找到here .任何帮助将不胜感激。
public class FileWatcher
{
private FileSystemWatcher _watcherRoot;
private Timer _timer;
private readonly string _watchedPath;
public FileWatcher(string path)
{
// _watcher = new FileSystemWatcher();
_timer = new Timer();
_watchedPath = path;
InitWatcher();
}
public void InitWatcher()
{
_watcherRoot = new FileSystemWatcher();
_watcherRoot.Path = _watchedPath;
_watcherRoot.IncludeSubdirectories = true;
_watcherRoot.EnableRaisingEvents = true;
_watcherRoot.Created += new FileSystemEventHandler(OnCreated);
}
private void OnCreated(object sender, FileSystemEventArgs e)
{
if (e.ChangeType == WatcherChangeTypes.Created)
{
string fullPath = e.FullPath;
if (sender == _watcherRoot)
{
// If detect new folder, set the timer to 5 sec
_timer.Interval = 5000;
_timer.Elapsed += OnTimedEvent;
_timer.AutoReset = true;
_timer.Enabled = true;
// a directory
Console.WriteLine($"{fullPath.ToString()} created on {DateTime.Now}");
}
}
}
private void OnTimedEvent(object sender, ElapsedEventArgs e)
{
// Create a 2nd Watcher??
// Reset the timer in here??
}
最佳答案
这里有一个简单的扩展方法来重置给定的计时器。
public static void Reset(this Timer timer)
{
timer.Stop();
timer.Start();
}
要从事件内部获取计时器对象,您需要将 sender
转换为 System.Timers.Timer()
或仅在静态上下文中使用计时器。
关于c# - 在 C# 中将计时器与 fileSystemWatcher 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53084587/
是否可以使用 FileSystemWatcher 查找正在更改文件的 PID 或进程名称? 最佳答案 不,您需要一个文件系统过滤器驱动程序来跟踪具有此类详细信息的更改。 关于filesystemwat
我有一个项目可以监视当前由 Windows Server 计算机托管的网络共享上的文件创建/删除事件,但我正在考虑将文件移动到运行 DataOntap 的 NetApp 机器上。他们的 API 文档说
我正在尝试跟踪文件系统的更改。因此我使用 FileSystemWatcher。不幸的是,Renamed 事件在 Windows 7 和 Windows 8 上不提供 oldName 信息。在 Wind
当我尝试监视网络路径上的文件夹(DFS - 分布式文件系统)时出现异常 System.IO.Internal.BufferOverflowException: To many changes at o
我想问一下是否有可能忽略由自己的进程 FileSystemWatcher 生成的文件系统更改事件正在运行。例如,我自己的进程在监视目录中创建了一个文件,并且FileSystemWatcher 应该认识
我正在使用 FileSystemWatcher 来监控文档扫描到的文件夹。当检测到新文件时,它会发送电子邮件通知某人。它按原样工作,但有时(不是每个文件)它会在新文件上触发 2 或 3 次,并为同一文
FileSystemWatcher 事件可以触发多次。如果我的代码需要可预测的行为,那就不好了。 这在 MSDN documentation 中有描述: Common file system oper
我在 Ubuntu VM 上有一个 dnx 控制台应用程序,它监视与主机操作系统(Windows 8.1)共享的文件夹。当 Ubuntu VM 上的共享文件夹中发生文件更改时,控制台应用程序会响应它们
我需要在我将使用 FileSystemWatcher 的文件夹中记录文件的创建或复制/移动事件。问题是,当我将一个文件粘贴到文件夹中时,FileSystemWatcher 将引发一个 Created
有些东西让我很感兴趣,我没有找到任何资源。 FileSystemWatcher 如何知道计算机“A”上的文件何时创建/删除/更改/重命名? 我认为这适用于轮询(观察者轮询服务器以检查更新),但在使用
我有一个正在监视新文件创建的文件夹。创建文件后,运行的任务可能需要5到30分钟。当最后一个文件仍在处理中时,通常可以创建一个新文件。 从一些测试案例来看,似乎有一些任务队列。有没有办法允许任务同时运行
您好,我正在创建一个 Windows 服务来监视某些目录,以查看目录的大小是否已达到其限制。我创建了一个文件系统观察器,如下所示: FileSystemWatcher watc
我设置了一个 FileSystemWatcher 来检查新文件、将内容存储在数据库中并删除文件。大约10天前,它开始忽略一些文件。我们讨论的是总共 50,000 个文件中的 1,500 个文件。通过手
我在代码中使用 FileSystemWatcher 来跟踪受监视目录下文件的任何更改/重命名/添加。现在,如果受监控的目录本身被删除,我需要一个通知。 关于如何实现这一目标有什么建议吗? 我试图在父目
您好,我正在创建一个 Windows 服务来监视某些目录,以查看目录的大小是否已达到其限制。我创建了一个文件系统观察器,如下所示: FileSystemWatcher watc
我目前正在为 OpenFOAM 输出文件实现文件内容观察器。这些文件由 OpenFOAM 在 Unix 环境中编写,并由我在 Windows 环境中的应用程序使用。 请考虑我的第一个工作收敛文件观察者
我写了下面的代码。 [XmlRoot("myxml")] public class myxml { [XmlElement("one")] public string one { g
我过去使用过 FileSystemWatcher。但是,我希望有人可以解释它在幕后的实际运作方式。 我计划在我正在制作的应用程序中使用它,它将监控大约 5 个驱动器和可能 300,000 个文件。 F
如何用 C# 将文件写入 FileSystemWatcher 监视的文件夹路径? 我的fileSystemWatcher设置如下: public FileSystemWatcher CreateAnd
我有以下代码用于监视文本文件的目录,该目录每天两次获取新文件,该代码在一段时间内工作正常但之后它停止触发 OnCreated 事件... [PermissionSet(SecurityAction.D
我是一名优秀的程序员,十分优秀!