From eba149dbd67c1c0d109afe1479a2bab7b914e0c7 Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Tue, 6 Oct 2020 16:13:28 +0200 Subject: [PATCH] Applicazione test x modifiche filesystem, prove fatte in Zanoni x Euromap63 --- TestApp/TestApp.sln => TestApp.sln | 0 TestApp/FSWatch.cs | 109 ++++++++++++++++++ TestApp/NLog.config | 54 +++++++++ TestApp/{TestApp => }/Program.cs | 8 +- .../{TestApp => }/Properties/AssemblyInfo.cs | 0 TestApp/{TestApp => }/TestApp.csproj | 25 ++++ TestApp/Utils.cs | 23 ++++ TestApp/logs/.placeholder | 1 + TestApp/packages.config | 4 + 9 files changed, 221 insertions(+), 3 deletions(-) rename TestApp/TestApp.sln => TestApp.sln (100%) create mode 100644 TestApp/FSWatch.cs create mode 100644 TestApp/NLog.config rename TestApp/{TestApp => }/Program.cs (50%) rename TestApp/{TestApp => }/Properties/AssemblyInfo.cs (100%) rename TestApp/{TestApp => }/TestApp.csproj (67%) create mode 100644 TestApp/Utils.cs create mode 100644 TestApp/logs/.placeholder create mode 100644 TestApp/packages.config diff --git a/TestApp/TestApp.sln b/TestApp.sln similarity index 100% rename from TestApp/TestApp.sln rename to TestApp.sln diff --git a/TestApp/FSWatch.cs b/TestApp/FSWatch.cs new file mode 100644 index 00000000..89f8715c --- /dev/null +++ b/TestApp/FSWatch.cs @@ -0,0 +1,109 @@ +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})"); + } +} \ No newline at end of file diff --git a/TestApp/NLog.config b/TestApp/NLog.config new file mode 100644 index 00000000..a6354580 --- /dev/null +++ b/TestApp/NLog.config @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/TestApp/TestApp/Program.cs b/TestApp/Program.cs similarity index 50% rename from TestApp/TestApp/Program.cs rename to TestApp/Program.cs index 5f7c5c27..5149f5b6 100644 --- a/TestApp/TestApp/Program.cs +++ b/TestApp/Program.cs @@ -5,10 +5,12 @@ using System.Text; namespace TestApp { - class Program + internal class Program { - static void Main(string[] args) + private static void Main(string[] args) { + // eseguo test FS Watcher + FSWatch.Run(); } } -} +} \ No newline at end of file diff --git a/TestApp/TestApp/Properties/AssemblyInfo.cs b/TestApp/Properties/AssemblyInfo.cs similarity index 100% rename from TestApp/TestApp/Properties/AssemblyInfo.cs rename to TestApp/Properties/AssemblyInfo.cs diff --git a/TestApp/TestApp/TestApp.csproj b/TestApp/TestApp.csproj similarity index 67% rename from TestApp/TestApp/TestApp.csproj rename to TestApp/TestApp.csproj index a278554a..dde66a30 100644 --- a/TestApp/TestApp/TestApp.csproj +++ b/TestApp/TestApp.csproj @@ -11,6 +11,8 @@ v4.0 512 true + + AnyCPU @@ -32,8 +34,15 @@ 4 + + ..\packages\NLog.4.7.5\lib\net40-client\NLog.dll + + + + + @@ -41,8 +50,24 @@ + + + + + Always + + + Always + + + + + + + + \ No newline at end of file diff --git a/TestApp/Utils.cs b/TestApp/Utils.cs new file mode 100644 index 00000000..473b5e49 --- /dev/null +++ b/TestApp/Utils.cs @@ -0,0 +1,23 @@ +using NLog; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace TestApp +{ + public class Utils + { + + /// + /// S + /// + public static Logger Log { get; private set; } + static Utils() + { + LogManager.ReconfigExistingLoggers(); + + Log = LogManager.GetCurrentClassLogger(); + } + } +} diff --git a/TestApp/logs/.placeholder b/TestApp/logs/.placeholder new file mode 100644 index 00000000..5f282702 --- /dev/null +++ b/TestApp/logs/.placeholder @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/TestApp/packages.config b/TestApp/packages.config new file mode 100644 index 00000000..73e35b24 --- /dev/null +++ b/TestApp/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file