/// <summary> /// Check for changes in a registered directory /// </summary> /// <param name="path">Path to watched directory</param> private static void check(string path) { var directory = getDirectory(path); var oldDirectory = getWatchedDirectoryFromFile(directory.FullName); // register the directory structure again var newDirectory = new WatchedDirectory(directory, oldDirectory.ComputeFileHashes); newDirectory.PopulateFiles(writeToConsole: false); var changes = oldDirectory.FindChanges(newDirectory); foreach (var change in changes) { Console.WriteLine(change.ToString()); } if (!changes.Any()) { Console.WriteLine("No changes were found."); } Console.WriteLine("Do you want to re-watch this directory? [y/n]"); if (Console.ReadLine().ToLower() == "y") { watch(directory.FullName, oldDirectory.ComputeFileHashes); } }
/// <summary> /// Register a directory for monitoring /// </summary> /// <param name="path">Path to directory</param> /// <param name="computeHash">Should we hash each watched file's contents?</param> private static void watch(string path, bool computeHash) { var directory = getDirectory(path); var watchedDirectory = new WatchedDirectory(directory, computeHash); // grab all files within the directory watchedDirectory.PopulateFiles(writeToConsole: true); Console.WriteLine("Serializing " + directory.FullName); string json = JsonConvert.SerializeObject(watchedDirectory); // save as a hash of the full path string filePath = getHashedFilePath(directory.FullName); Console.WriteLine("Writing to " + filePath); File.WriteAllText(filePath, json); // update our dictionay entry for this directory or make a new one if (watchedDirectories.ContainsKey(directory.FullName)) { watchedDirectories[directory.FullName] = DateTime.Now; } else { watchedDirectories.Add(directory.FullName, DateTime.Now); } Console.WriteLine("Done."); }