Search code examples
c#asp.netasp.net-corefilesystemwatcher

Is there a "folder fingerprint" to detect if a folder's contents have changed between two points in time? I cannot use FileSystemWatcher


I am looking for the fastest way (in C#/ASP.NET) to detect if anything inside a folder has changed between two points in time. I don't need to know the files, total size, or anything else about the folder. I just need to know if anything has changed.

Because I do not have access to the outer scope in which such a folder change detection method would run, I cannot use FileSystemWatcher, which must be disposed when no longer needed (it implements IDisposable and due to the architecture of what I need I am unable to use it in a practical manner).

It would be ideal if there were some kind of "folder fingerprint" GUID that changed if the folder's contents changed. I could then just compare the last fingerprint GUID to the current fingerprint and return true if the two fingerprints were different, indicating something has changed within the folder.

Ideally it would work exactly like the value returned by Assembly.GetEntryAssembly().ManifestModule.ModuleVersionId, which gives you a "fingerprint" GUID of your application's current build, which then allows you to detect if your application has been recompiled between two points in time.

Something like this:

Note: "System.IO.GetFolderFingerprint()" is an imaginary method.

public static class FolderChangeChecker
{
    private const string _path = "c:/path/to/folder";    
    private static Guid _originalFingerprint = System.IO.GetFolderFingerprint(_path);    
    public static bool FolderHasChanged()
    {    
        return System.IO.GetFolderFingerprint(_path) != _originalFingerprint;
    }
}

Please let me know if there is a way to do this quickly.


Solution

  • After some testing, I found that the DateTime returned by System.IO.File.GetLastWriteTime("c:/path/to/folder") is updated every time a file inside the folder is changed. So, this works perfect for what I needed.

    To add to Neil W's answer, theoretically, you could create a hash from the concatenation of the last write time and the folder path, that works without having to enumerate all the files inside the folder.