目录修改监控

发布于 2024-07-06 00:10:50 字数 172 浏览 6 评论 0原文

我正在构建一个 C# 应用程序,它将监视指定目录的更改和添加,并将信息存储在数据库中。

我想避免检查每个单独的文件是否有修改,但我不确定是否可以完全信任文件访问时间。

在目录中获取最近修改的文件的最佳方法是什么?

它只会在用户要求时检查修改,它不会是一个持续运行的服务。

I'm building a C# application that will monitor a specified directory for changes and additions and storing the information in a database.

I would like to avoid checking each individual file for modifications, but I'm not sure if I can completely trust the file access time.

What would be the best method to use for getting recently modified files in a directory?

It would check for modifications only when the user asks it to, it will not be a constantly running service.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

唯憾梦倾城 2024-07-13 00:10:50

使用 FileSystemWatcher 对象。 这里有一些代码可以完成您正在寻找的操作。

    // Declares the FileSystemWatcher object
    FileSystemWatcher watcher = new FileSystemWatcher(); 

    // We have to specify the path which has to monitor

     watcher.Path = @"\\somefilepath";     

    // This property specifies which are the events to be monitored
     watcher.NotifyFilter = NotifyFilters.LastAccess |
       NotifyFilters.LastWrite | NotifyFilters.FileName | notifyFilters.DirectoryName; 
    watcher.Filter = "*.*"; // Only watch text files.

    // Add event handlers for specific change events...

    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;


    // Define the event handlers.
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
    // Specify what is done when a file is changed, created, or deleted.
    }

    private static void OnRenamed(object source, RenamedEventArgs e)
    {
    // Specify what is done when a file is renamed.
    }

Use the FileSystemWatcher object. Here is some code to do what you are looking for.

    // Declares the FileSystemWatcher object
    FileSystemWatcher watcher = new FileSystemWatcher(); 

    // We have to specify the path which has to monitor

     watcher.Path = @"\\somefilepath";     

    // This property specifies which are the events to be monitored
     watcher.NotifyFilter = NotifyFilters.LastAccess |
       NotifyFilters.LastWrite | NotifyFilters.FileName | notifyFilters.DirectoryName; 
    watcher.Filter = "*.*"; // Only watch text files.

    // Add event handlers for specific change events...

    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;


    // Define the event handlers.
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
    // Specify what is done when a file is changed, created, or deleted.
    }

    private static void OnRenamed(object source, RenamedEventArgs e)
    {
    // Specify what is done when a file is renamed.
    }
我恋#小黄人 2024-07-13 00:10:50

嗯...有趣的问题。 首先,我会向您指出 FileSystemWatcher 类。 但是,如果您想让它根据用户请求工作,那么您似乎可能需要最初存储目录信息,然后在每次用户请求时进行比较。 我可能会选择 FileSystemWatcher 并存储结果。

Hmm... interesting question. Initially I'd point you at the FileSystemWatcher class. If you are going to have it work, however, on user request, then it would seem you might need to store off the directory info initially and then compare each time the user requests. I'd probably go with a FileSystemWatcher and just store off your results anyhow.

蓝咒 2024-07-13 00:10:50

我认为您想要的由 FileSystemWatcher 提供班级。

本教程介绍如何使用它来监视简单 Windows 服务中目录的更改; 如何用 C# 实现简单的 filewatcher Windows 服务

I think what you want is provided by the FileSystemWatcher class.

This tutorial describes how to use it to monitor a directory for changes in a simple windows service; How to implement a simple filewatcher Windows service in C#

独﹏钓一江月 2024-07-13 00:10:50

如果您只需要它在用户询问时而不是一直进行检查,请不要使用 FileSystemWatcher。 特别是如果它是共享资源 - 您最不希望看到的是 50 台客户端计算机监视同一个共享目录。

这可能只是一个拼写错误,但您不应该查看文件访问时间,您应该查看文件修改时间以获取更改。 即使这样也不可靠。

我要做的是对文件日期和字节大小或其他文件系统属性实现某种校验和函数。 这样我就不会在整个文件中查找更改 - 仅查找它的属性,并且我可以根据请求执行此操作,而不是尝试保持与远程资源的连接来监视它。

更重的解决方案是反其道而行之,并在托管共享驱动器的计算机上安装一项服务,该服务可以监视文件并记录更改。 然后你可以查询服务而不用接触文件——但这可能有点过头了。

If you only need it to check when the user asks rather then all the time, don't use the FileSystemWatcher. Especially if it's a shared resource - the last thing you want is 50 client machines watching the same shared directory.

It's probably just a typo, but you shouldn't be looking at the file access time, you want to look at the file modification time to pick up changes. Even that's not reliable.

What I would do is implement some sort of checksum function on the file date and byte size, or other file system properties. That way I wouldn't be looking for changes in the complete file - only the properties of it and I can do it on request, rather than trying to hold a connection to a remote resource to monitor it.

A heavier solution would be to do it the other way around, and install a service on the machine hosting the shared drive which could monitor the files and make note of the changes. Then you could query the service rather than touching the files at all - but it's probably overkill.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文