From 9298a44f0e02a66f2d98c3ec140a92f59ea22946 Mon Sep 17 00:00:00 2001 From: Dylan George Field <13308154+dylan-george-field@users.noreply.github.com> Date: Sun, 22 Jan 2023 21:22:37 +1100 Subject: [PATCH] fix: bug where directory won't be created if it doesn't exist --- src/wfh-log-wpf/App.xaml.cs | 7 +------ src/wfh-log-wpf/Logger/BaseLog.cs | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/wfh-log-wpf/App.xaml.cs b/src/wfh-log-wpf/App.xaml.cs index 97d3fad..55238d9 100644 --- a/src/wfh-log-wpf/App.xaml.cs +++ b/src/wfh-log-wpf/App.xaml.cs @@ -1,18 +1,13 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Microsoft.Win32; using System; using System.ComponentModel; using System.Diagnostics; -using System.IO; -using System.Linq; using System.Reflection; using System.Windows; using wfh_log_wpf.Logger; -using wfh_log_wpf.Models; using wfh_log_wpf.Settings; using wfh_log_wpf.Timer; -using wfh_log_wpf.Uninstaller; namespace wfh_log_wpf { @@ -114,7 +109,7 @@ private void ShowMainWindow() private void OpenLogFile() { - var absoluteFilePath = BaseLog.GetLogPath(); + var absoluteFilePath = BaseLog.GetLogFilePath(); var processStartInfo = new ProcessStartInfo { diff --git a/src/wfh-log-wpf/Logger/BaseLog.cs b/src/wfh-log-wpf/Logger/BaseLog.cs index 2faab8f..4b28b0b 100644 --- a/src/wfh-log-wpf/Logger/BaseLog.cs +++ b/src/wfh-log-wpf/Logger/BaseLog.cs @@ -12,16 +12,24 @@ public abstract class BaseLog public BaseLog() { - AbsoluteFilePath = GetLogPath(); + AbsoluteFilePath = GetLogFilePath(); + + Directory.CreateDirectory(GetLogDirectoryPath()); } - public static string GetLogPath() + public static string GetLogFilePath() + { + var filename = "wfh.log"; + + return Path.Combine(GetLogDirectoryPath(), filename); + } + + public static string GetLogDirectoryPath() { var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); var directory = "wfh-log"; - var filename = "wfh.log"; - return Path.Combine(appDataPath, directory, filename); + return Path.Combine(appDataPath, directory); } } }