diff --git a/Test-FileWatcher.sln b/Test-FileWatcher.sln new file mode 100644 index 0000000..d10de4b --- /dev/null +++ b/Test-FileWatcher.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30413.136 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test-FileWatcher", "Test-FileWatcher\Test-FileWatcher.csproj", "{41CED9A1-A4F8-4F8D-97EC-CBEFC2B3F2EB}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {41CED9A1-A4F8-4F8D-97EC-CBEFC2B3F2EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {41CED9A1-A4F8-4F8D-97EC-CBEFC2B3F2EB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {41CED9A1-A4F8-4F8D-97EC-CBEFC2B3F2EB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {41CED9A1-A4F8-4F8D-97EC-CBEFC2B3F2EB}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {D37298AA-67AF-432E-B3BB-12C51CD28F92} + EndGlobalSection +EndGlobal diff --git a/Test-FileWatcher/FSWatch.cs b/Test-FileWatcher/FSWatch.cs new file mode 100644 index 0000000..89f8715 --- /dev/null +++ b/Test-FileWatcher/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/Test-FileWatcher/NLog.config b/Test-FileWatcher/NLog.config new file mode 100644 index 0000000..a635458 --- /dev/null +++ b/Test-FileWatcher/NLog.config @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/Test-FileWatcher/Program.cs b/Test-FileWatcher/Program.cs new file mode 100644 index 0000000..5149f5b --- /dev/null +++ b/Test-FileWatcher/Program.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace TestApp +{ + internal class Program + { + private static void Main(string[] args) + { + // eseguo test FS Watcher + FSWatch.Run(); + } + } +} \ No newline at end of file diff --git a/Test-FileWatcher/Properties/AssemblyInfo.cs b/Test-FileWatcher/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..3edf227 --- /dev/null +++ b/Test-FileWatcher/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Le informazioni generali relative a un assembly sono controllate dal seguente +// set di attributi. Modificare i valori di questi attributi per modificare le informazioni +// associate a un assembly. +[assembly: AssemblyTitle("TestApp")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("TestApp")] +[assembly: AssemblyCopyright("Copyright © 2020")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Se si imposta ComVisible su false, i tipi in questo assembly non saranno visibili +// ai componenti COM. Se è necessario accedere a un tipo in questo assembly da +// COM, impostare su true l'attributo ComVisible per tale tipo. +[assembly: ComVisible(false)] + +// Se il progetto viene esposto a COM, il GUID seguente verrà utilizzato come ID della libreria dei tipi +[assembly: Guid("41ced9a1-a4f8-4f8d-97ec-cbefc2b3f2eb")] + +// Le informazioni sulla versione di un assembly sono costituite dai seguenti quattro valori: +// +// Versione principale +// Versione secondaria +// Numero di build +// Revisione +// +// È possibile specificare tutti i valori oppure impostare valori predefiniti per i numeri relativi alla revisione e alla build +// usando l'asterisco '*' come illustrato di seguito: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Test-FileWatcher/Test-FileWatcher.csproj b/Test-FileWatcher/Test-FileWatcher.csproj new file mode 100644 index 0000000..dde66a3 --- /dev/null +++ b/Test-FileWatcher/Test-FileWatcher.csproj @@ -0,0 +1,73 @@ + + + + + Debug + AnyCPU + {41CED9A1-A4F8-4F8D-97EC-CBEFC2B3F2EB} + Exe + TestApp + TestApp + v4.0 + 512 + true + + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\NLog.4.7.5\lib\net40-client\NLog.dll + + + + + + + + + + + + + + + + + + + + + + Always + + + Always + + + + + + + + + + \ No newline at end of file diff --git a/Test-FileWatcher/Utils.cs b/Test-FileWatcher/Utils.cs new file mode 100644 index 0000000..473b5e4 --- /dev/null +++ b/Test-FileWatcher/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/Test-FileWatcher/logs/.placeholder b/Test-FileWatcher/logs/.placeholder new file mode 100644 index 0000000..5f28270 --- /dev/null +++ b/Test-FileWatcher/logs/.placeholder @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Test-FileWatcher/packages.config b/Test-FileWatcher/packages.config new file mode 100644 index 0000000..73e35b2 --- /dev/null +++ b/Test-FileWatcher/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file