简体   繁体   中英

How to determine if a Directory/Folder has been changed

I have a folder with the following structure

Parent/
    Child1/
        GrandChild1/
             File1.txt

I need to query Parent folder and find out if Child1 has changed.

Changed = A new file was add/update/deleted.

The Child1 folder DateModified is not updated. Only the GrandChild1 date modified was updated when changes occurs. I am trying to avoid going to the file level to determine if the rootparent has changed. since there will be many folders and sub folder. I just need to know if Child1 has changed.

I do not want to use FileSystemWatcher, since I am running this as a scheduled job and not watching it LIVE.

User FileSystemWatcher. Remember to enable raising events since it is a common mistake (watchfolder.EnableRaisingEvents = true;).

The FileSystemWatcher may prove not to be optimal from a performance perspective. If that is an issue for you, you might implement a CRC check with a Timer to check for changes of the files and folders you are interested in.

Essentially, what I would do is to generate a CRC32 hash for the entire folder I am watching (and save it away into variable A) and when I decide it is time to check for changes, you simply calculate a new CRC32 hash for the same folder (into variable B). You then compare A with B and if they don´t match, something has changed. Really not that difficult.

Reference:

Have you tried the file system watcher? You can monitor local drives for changes from a given path, and then if necessary, ignore or process the fact they changed.

You can use the FileSystemWatcher Class for this.

Alternatively, if you would rather schedule a Task to run, Weekly, for example, you might want to have a look at: http://taskscheduler.codeplex.com/ and http://www.emoreau.com/Entries/Articles/2004/08/Interfacing-the-Windows-Task-Scheduler.aspx

And here's a link to the Windows Task Schedular API

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM