检测目录更改 [英] Detect changes in directory

查看:126
本文介绍了检测目录更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要监控的任何文件操作(添加/删除/重命名)的文件夹及其子目录。我读过有关FileSystemWatcher的,但我想监控每一个时间之间变化的程序运行或当用户按下检查更改按钮(FSW似乎更定向到运行时检测)。我首先想到的是通过所有的(子)目录遍历和哈希每个文件。然后连接所有哈希(已订购)和哈希这一点。当我想检查的变化,我重复这个过程,并检查哈希值是相同的。

I need to monitor a folder and its subdirectories for any file manipulations (add/remove/rename). I've read about FileSystemWatcher but I'd like to monitor changes between each time the program is run or when the user presses the "check for changes" button (FSW seems more orientated to runtime detection). My first thought was to iterate through all the (sub)directories and hash each file. Then, concatenate all the hashes (which have been ordered) and hash that. When I want to check for changes, I repeat the process and check if the hashes are the same.

这是做的有效途径?
此外,一旦我发现一个变化,我怎么发现文件已经被添加,删除或重命名哪些尽快?

Is this an efficient way of doing it? Also, once I've detected a change, how do I find out what file has been added, removed or renamed as quickly as possible?

作为侧面说明,我不介意使用脚本,如果他们只要快那些脚本不需要最终用户安装任何东西做到这一点,该脚本可以通知我的C#的变化的应用程序。

As a side note, I don't mind using scripts to do this if they're faster as long as those scripts don't require end users to install anything and the scripts can notify my C# app of the changes.

推荐答案

我们通过存储在数据库中找到的所有文件,其最后修改时间一起处理这个问题。

We handle this by storing all found files in a database along with their last modification time.

在每次通过文件,我们检查数据库的每个文件:如果它不在DB存在,它是新的,如果它确实存在,但时间戳是不同的,它已经改变了。

On each pass through the files, we check the database for each file: if it doesn't exist in the DB, it is new and if it does exist, but the timestamp is different, it has changed.

还有通过标记的所有文件中通和清算之前到数据库ToBeDeleteed处理删除文件的选项如果这个文件被发现。然后,在过程结束时,我们可以删除所有的被标记为ToBeDeleted记录

There is also an option to handle deleted files by marking all of the files in the database as ToBeDeleteed prior to the pass and clearing this if the file was found. Then, at the end of the process, we can just delete all of the records that are marked as ToBeDeleted.

这篇关于检测目录更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆