diff --git a/Source/Monitorian.Core/AppKeeper.cs b/Source/Monitorian.Core/AppKeeper.cs index b6a11c1b..69e93ec9 100644 --- a/Source/Monitorian.Core/AppKeeper.cs +++ b/Source/Monitorian.Core/AppKeeper.cs @@ -113,7 +113,7 @@ private async Task ParseArgumentsAsync(StartupEventArgs e, string[] standardOpti public Task LoadArgumentsAsync() => AppDataService.ReadAsync(ArgumentsFileName); - public Task SaveArgumentsAsync(string content) => AppDataService.WriteAsync(ArgumentsFileName, false, content); + public Task SaveArgumentsAsync(string content) => AppDataService.WriteAsync(ArgumentsFileName, append: false, delete: true, content); #endregion diff --git a/Source/Monitorian.Core/Models/AppDataService.cs b/Source/Monitorian.Core/Models/AppDataService.cs index f2a71bda..17378e36 100644 --- a/Source/Monitorian.Core/Models/AppDataService.cs +++ b/Source/Monitorian.Core/Models/AppDataService.cs @@ -44,10 +44,23 @@ public static async Task ReadAsync(string fileName) return await sr.ReadToEndAsync(); } - public static async Task WriteAsync(string fileName, bool append, string content) + /// + /// Asynchronously writes to a specified file. + /// + /// File name to write to + /// True to append to the file; False to overwrite the file + /// True to delete the file if content is null or empty; False to leave the file + /// Content to write to the file + public static async Task WriteAsync(string fileName, bool append, bool delete, string content) { var filePath = Path.Combine(EnsureFolderPath(), fileName); + if (delete && string.IsNullOrEmpty(content)) + { + File.Delete(filePath); + return; + } + using var sw = new StreamWriter(filePath, append, Encoding.UTF8); // BOM will be emitted. await sw.WriteAsync(content); } diff --git a/Source/Monitorian.Core/Models/Logger.cs b/Source/Monitorian.Core/Models/Logger.cs index 1f178fc3..9b4809f8 100644 --- a/Source/Monitorian.Core/Models/Logger.cs +++ b/Source/Monitorian.Core/Models/Logger.cs @@ -105,7 +105,7 @@ private static async Task GetOperationFileNamesAsync() if (fileNames.Any()) { content += await AppDataService.ReadAsync(fileNames.First()); - await AppDataService.WriteAsync(fileNames.First(), append: false, content); + await AppDataService.WriteAsync(fileNames.First(), append: false, delete: false, content); AppDataService.Delete(OperationFileName); } else @@ -134,7 +134,7 @@ public static async Task RecordOperationAsync(string content) try { - await AppDataService.WriteAsync(fileName, append: true, content); + await AppDataService.WriteAsync(fileName, append: true, delete: false, content); } catch (Exception ex) {