【问题标题】:App that monitors changes in a folder and reacts to them [closed]监视文件夹中的更改并对其做出反应的应用程序[关闭]
【发布时间】:2015-08-14 09:34:14
【问题描述】:

我必须编写一个应用程序,在身份验证阶段之后,它会监视文件夹的更改,并在发生某些事情时(添加/删除/更新文件)通过向服务器发送通知来做出反应。

最好的方法是什么?

身份验证后启动的 Windows 服务?

请注意,此监控活动应在用户浏览 ui 的同时执行。

【问题讨论】:

标签: c# .net windows windows-services monitoring


【解决方案1】:

我建议你使用FileSystemWatcher来监控目录的变化。您必须将 System.IO; 导入您的项目才能实现此功能。

 FileSystemWatcher watcher = new FileSystemWatcher("your directory path");

您可以根据目录的变化来分配事件,如下所示:

watcher.Changed += new FileSystemEventHandler(OnChanged);//<-- call OnChanged when the contents changed
watcher.Created += new FileSystemEventHandler(OnChanged);//<-- call OnChanged when new files are created
watcher.Deleted += new FileSystemEventHandler(OnChanged);//<-- call OnChanged when any file is deleted
watcher.Renamed += new RenamedEventHandler(OnRenamed);//<-- call OnChanged when any file is renamed

private static void OnChanged(object source, FileSystemEventArgs e)
    {
       // Define your method here
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多