Snapshot directories and detect added, removed, or modified files between runs using SHA-256 hashing.
dotnet add package Philiprehberger.FilesystemSnapshotSnapshot directories and detect added, removed, or modified files between runs using SHA-256 hashing.
dotnet add package Philiprehberger.FilesystemSnapshot
using Philiprehberger.FilesystemSnapshot;
// Take a snapshot
var before = DirectorySnapshot.Take("/path/to/dir");
// ... time passes, files change ...
var after = DirectorySnapshot.Take("/path/to/dir");
SnapshotDiff diff = before.CompareTo(after);
foreach (var f in diff.Added) Console.WriteLine($"+ {f}");
foreach (var f in diff.Removed) Console.WriteLine($"- {f}");
foreach (var f in diff.Modified) Console.WriteLine($"~ {f}");
// Save before exiting
var snapshot = DirectorySnapshot.Take("/path/to/dir");
snapshot.SaveTo("snapshot.json");
// Restore next time
var previous = DirectorySnapshot.LoadFrom("snapshot.json");
var diff = previous.CompareTo(DirectorySnapshot.Take("/path/to/dir"));
var options = new SnapshotOptions
{
SearchPattern = "*.cs",
Recursive = true,
ExcludePatterns = new[] { @"^bin/", @"^obj/" }
};
var snapshot = DirectorySnapshot.Take("/path/to/dir", options);
DirectorySnapshot| Member | Description |
|---|---|
Files | IReadOnlyDictionary<string, string> — relative path to SHA-256 hash |
Timestamp | UTC time the snapshot was taken |
Take(path, options?) | Scan a directory and return a snapshot |
CompareTo(other) | Diff this snapshot against a newer one |
SaveTo(filePath) | Serialise to JSON |
LoadFrom(filePath) | Deserialise from JSON |
SnapshotOptions| Property | Default | Description |
|---|---|---|
SearchPattern | "*" | File name glob |
Recursive | true | Include sub-directories |
ExcludePatterns | null | Regex patterns for paths to skip |
SnapshotDiff| Property | Description |
|---|---|
Added | Files present in the newer snapshot only |
Removed | Files present in the older snapshot only |
Modified | Files present in both with different hashes |
dotnet build src/Philiprehberger.FilesystemSnapshot.csproj --configuration Release
If you find this project useful: