当前位置:  开发笔记 > 编程语言 > 正文

目录修改监控

如何解决《目录修改监控》经验,为你挑选了1个好方法。

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

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

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

它只会在用户询问时检查修改,它不会是一个持续运行的服务.



1> MikeJ..:

使用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.
    }

推荐阅读
喜生-Da
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有