using System; using System.IO; using System.Security.Permissions; namespace TestApp { /************************************************** * Classe gestione verifica cambio files in directory * * links: * https://docs.microsoft.com/it-it/dotnet/api/system.io.filesystemwatcher?view=netframework-4.0 * https://www.c-sharpcorner.com/UploadFile/puranindia/filesystemwatcher-in-C-Sharp/ * * * ***********************************************/ public static class FSWatch { /// /// Metodo principale esecuzione check filesystem /// [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] public static void Run() { //string[] args = Environment.GetCommandLineArgs(); //// If a directory is not specified, exit program. //if (args.Length != 2) //{ // // Display the proper way to call the program. // Console.WriteLine("Usage: Watcher.exe (directory)"); // return; //} try { // Create a new FileSystemWatcher and set its properties. using (FileSystemWatcher watcher = new FileSystemWatcher()) { //watcher.Path = args[1]; watcher.Path = Directory.GetCurrentDirectory(); // NO guardo le subdirs watcher.IncludeSubdirectories = false; // Watch for changes in LastAccess and LastWrite times, and // the renaming of files or directories. //watcher.NotifyFilter = NotifyFilters.DirectoryName // | NotifyFilters.FileName // | NotifyFilters.LastAccess // | NotifyFilters.LastWrite // | NotifyFilters.Size; watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size; // Only watch text files. watcher.Filter = "*.*"; // Add event handlers. watcher.Changed += OnChanged; watcher.Created += OnChanged; watcher.Error += OnError; watcher.Deleted += OnChanged; watcher.Renamed += OnRenamed; // Begin watching. watcher.EnableRaisingEvents = true; // Wait for the user to quit the program. Console.WriteLine("Press 'q' to quit the sample."); while (Console.Read() != 'q') ; } } catch (IOException e) { //Console.WriteLine("A Exception Occurred :" + e); Utils.Log.Error("A Exception Occurred :" + e); } catch (Exception oe) { //Console.WriteLine("An Exception Occurred :" + oe); Utils.Log.Error("An Exception Occurred :" + oe); } } private static void OnError(object sender, ErrorEventArgs e) => // Specify what is done when a file is changed, created, or deleted. Console.WriteLine($"{DateTime.Now} | Error: {e.GetException()}"); // Define the event handlers. private static void OnChanged(object source, FileSystemEventArgs e) => Utils.Log.Info($"File: {e.Name} {e.ChangeType}"); // Specify what is done when a file is changed, created, or deleted. //Console.WriteLine($"{DateTime.Now} | File: {e.Name} {e.ChangeType} ({e.FullPath})"); private static void OnRenamed(object source, RenamedEventArgs e) => Utils.Log.Info($"File: {e.OldName} renamed to {e.Name}"); // Specify what is done when a file is renamed. //Console.WriteLine($"{DateTime.Now} | File: {e.OldName} renamed to {e.Name} ({e.OldFullPath} --> {e.FullPath})"); } }