From 44666d845058632ac650cadd6a727a115389d306 Mon Sep 17 00:00:00 2001 From: mammo0 Date: Mon, 6 Dec 2021 09:45:39 +0100 Subject: [PATCH] apply the new source code --- NetworkMiner/FileUtils.cs | 35 ++++++++++++++++++++++ NetworkMiner/NetworkMiner.csproj | 1 + NetworkMiner/Program.cs | 39 +++++++++++++------------ NetworkMiner/Properties/AssemblyInfo.cs | 4 +-- 4 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 NetworkMiner/FileUtils.cs diff --git a/NetworkMiner/FileUtils.cs b/NetworkMiner/FileUtils.cs new file mode 100644 index 0000000..558b7ab --- /dev/null +++ b/NetworkMiner/FileUtils.cs @@ -0,0 +1,35 @@ +using System; +using System.IO; +using System.Threading.Tasks; + +namespace NetworkMiner +{ + public static class FileUtils + { + public static async Task DeleteDirectory(string directoryPath, int maxRetries = 10, int millisecondsDelay = 30) + { + if (directoryPath == null) + throw new ArgumentNullException(directoryPath); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) { + try { + // try to delete the directory if it exists + if (Directory.Exists(directoryPath)) + Directory.Delete(directoryPath, true); + + return true; + } catch (IOException) { // System.IO.IOException: The directory is not empty + await Task.Delay(millisecondsDelay); + } catch (UnauthorizedAccessException) { + await Task.Delay(millisecondsDelay); + } + } + + return false; + } + } +} diff --git a/NetworkMiner/NetworkMiner.csproj b/NetworkMiner/NetworkMiner.csproj index d359bf8..14047b8 100644 --- a/NetworkMiner/NetworkMiner.csproj +++ b/NetworkMiner/NetworkMiner.csproj @@ -30,6 +30,7 @@ + diff --git a/NetworkMiner/Program.cs b/NetworkMiner/Program.cs index 34fa31e..44da2ae 100644 --- a/NetworkMiner/Program.cs +++ b/NetworkMiner/Program.cs @@ -18,7 +18,7 @@ namespace NetworkMiner { public static class Program { - private static string EXE_PATH = Path.GetFullPath(Assembly.GetEntryAssembly().Location); + private static readonly string EXE_PATH = Path.GetFullPath(Assembly.GetEntryAssembly().Location); /// /// The main entry point for the application. @@ -59,39 +59,40 @@ static async Task Main(string[] args) { public static void SetupLogger(string applicationName) { - Logger.ApplicationName = applicationName; + SharedUtils.Logger.ApplicationName = applicationName; foreach (string arg in Environment.GetCommandLineArgs()) { if (arg.Equals("--debug", StringComparison.InvariantCultureIgnoreCase)) { - Logger.CurrentLogLevel = Logger.LogLevel.Debug; - Logger.LogToConsole = true; + SharedUtils.Logger.CurrentLogLevel = SharedUtils.Logger.LogLevel.Debug; + SharedUtils.Logger.LogToConsole = true; } else if (arg.Equals("--eventlog", StringComparison.InvariantCultureIgnoreCase)) { - EventLog applicationEventLog = new EventLog("Application"); + System.Diagnostics.EventLog applicationEventLog = new System.Diagnostics.EventLog("Application"); applicationEventLog.Source = applicationName; - Logger.CurrentLogLevel = Logger.LogLevel.Debug; - Logger.EnableEventLog((message, eventLogEntryType) => applicationEventLog.WriteEntry(message, (EventLogEntryType)eventLogEntryType)); + SharedUtils.Logger.CurrentLogLevel = SharedUtils.Logger.LogLevel.Debug; + SharedUtils.Logger.EnableEventLog((message, eventLogEntryType) => applicationEventLog.WriteEntry(message, (System.Diagnostics.EventLogEntryType)eventLogEntryType)); } else if (arg.Equals("--filelog", StringComparison.InvariantCultureIgnoreCase)) { - Logger.CurrentLogLevel = Logger.LogLevel.Debug; - Logger.LogToFile = true; + SharedUtils.Logger.CurrentLogLevel = SharedUtils.Logger.LogLevel.Debug; + SharedUtils.Logger.LogToFile = true; } } #if DEBUG - Logger.CurrentLogLevel = Logger.LogLevel.Debug; - Logger.LogToConsole = true; - Logger.LogToFile = true; + SharedUtils.Logger.CurrentLogLevel = SharedUtils.Logger.LogLevel.Debug; + SharedUtils.Logger.LogToConsole = true; + //SharedUtils.Logger.EnableEventLog(); + SharedUtils.Logger.LogToFile = true; #endif FileVersionInfo productInfo = FileVersionInfo.GetVersionInfo(EXE_PATH); - Logger.Log("Environment.Is64BitOperatingSystem = " + Environment.Is64BitOperatingSystem.ToString(), Logger.EventLogEntryType.Information); - Logger.Log("Environment.Is64BitProcess = " + Environment.Is64BitProcess.ToString(), Logger.EventLogEntryType.Information); - Logger.Log(productInfo.ProductName + " " + productInfo.ProductVersion, Logger.EventLogEntryType.Information); - Logger.Log("Application.ExecutablePath = " + EXE_PATH, Logger.EventLogEntryType.Information); - Logger.Log("Application.CurrentCulture = " + CultureInfo.CurrentCulture, Logger.EventLogEntryType.Information); - Logger.Log("Environment.Version = " + Environment.Version, Logger.EventLogEntryType.Information);//4.0.30319.42000 = .NET Framework 4.6, its point releases, and the .NET Framework 4.7 - Logger.Log("Starting application", Logger.EventLogEntryType.Information); + SharedUtils.Logger.Log("Environment.Is64BitOperatingSystem = " + Environment.Is64BitOperatingSystem.ToString(), SharedUtils.Logger.EventLogEntryType.Information); + SharedUtils.Logger.Log("Environment.Is64BitProcess = " + Environment.Is64BitProcess.ToString(), SharedUtils.Logger.EventLogEntryType.Information); + SharedUtils.Logger.Log(productInfo.ProductName + " " + productInfo.ProductVersion, SharedUtils.Logger.EventLogEntryType.Information); + SharedUtils.Logger.Log("Application.ExecutablePath = " + EXE_PATH, SharedUtils.Logger.EventLogEntryType.Information); + SharedUtils.Logger.Log("Application.CurrentCulture = " + CultureInfo.CurrentCulture, SharedUtils.Logger.EventLogEntryType.Information); + SharedUtils.Logger.Log("Environment.Version = " + Environment.Version, SharedUtils.Logger.EventLogEntryType.Information);//4.0.30319.42000 = .NET Framework 4.6, its point releases, and the .NET Framework 4.7 + SharedUtils.Logger.Log("Starting application", SharedUtils.Logger.EventLogEntryType.Information); } diff --git a/NetworkMiner/Properties/AssemblyInfo.cs b/NetworkMiner/Properties/AssemblyInfo.cs index 0d799b3..517e8c9 100644 --- a/NetworkMiner/Properties/AssemblyInfo.cs +++ b/NetworkMiner/Properties/AssemblyInfo.cs @@ -29,5 +29,5 @@ // Build Number // Revision // -[assembly: AssemblyVersion("2.6.0")] -[assembly: AssemblyFileVersion("2.6.0")] +[assembly: AssemblyVersion("2.7.1")] +[assembly: AssemblyFileVersion("2.7.1")]