From b3f936c194643cab92df01468371240dfbc14d71 Mon Sep 17 00:00:00 2001 From: xRushG <145561288+xRushG@users.noreply.github.com> Date: Thu, 23 May 2024 21:29:35 +0200 Subject: [PATCH] - Added my missed comment for outlined LocalSettingsManager - Fixed a bug that occurred when trying to open the log file on the Notification settings page. - Removed unnecessary file association when user tries to open the log file on the Notification page. The "choose file" dialog handles the extension as it is fixed on .log - Implemented fallback to open file explorer to the specified file location if mRem fails to open the log file. --- mRemoteNG/App/ProgramRoot.cs | 6 ++ .../Forms/OptionsPages/NotificationsPage.cs | 77 ++++++++++++++++++- 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/mRemoteNG/App/ProgramRoot.cs b/mRemoteNG/App/ProgramRoot.cs index c5891894..29023244 100644 --- a/mRemoteNG/App/ProgramRoot.cs +++ b/mRemoteNG/App/ProgramRoot.cs @@ -21,6 +21,12 @@ namespace mRemoteNG.App [STAThread] public static void Main(string[] args) { + /* + * Temporarily disable LocalSettingsManager initialization at startup + * due to unfinished implementation causing build errors. + * Uncomment if needed in your local repo. + */ + /* /*var settingsManager = new LocalSettingsManager(); // Check if the database exists diff --git a/mRemoteNG/UI/Forms/OptionsPages/NotificationsPage.cs b/mRemoteNG/UI/Forms/OptionsPages/NotificationsPage.cs index 159dee05..7a40c08d 100644 --- a/mRemoteNG/UI/Forms/OptionsPages/NotificationsPage.cs +++ b/mRemoteNG/UI/Forms/OptionsPages/NotificationsPage.cs @@ -155,8 +155,15 @@ namespace mRemoteNG.UI.Forms.OptionsPages private void buttonOpenLogFile_Click(object sender, System.EventArgs e) { - if (Path.GetExtension(textBoxLogPath.Text) == ".log") - Process.Start(textBoxLogPath.Text); + string logFile = textBoxLogPath.Text; + bool doesExist = File.Exists(logFile); + + if (doesExist && OpenLogAssociated(logFile)) + return; + else if (doesExist && OpenLogNotepad(logFile)) + return; + + OpenLogLocation(logFile); } private void chkLogToCurrentDir_CheckedChanged(object sender, System.EventArgs e) @@ -165,5 +172,71 @@ namespace mRemoteNG.UI.Forms.OptionsPages buttonRestoreDefaultLogPath.Enabled = !chkLogToCurrentDir.Checked; textBoxLogPath.Text = Logger.DefaultLogPath; } + + #region Privat Methods to Open Logfile + + /// + /// Attempts to open a file using the default application associated with its file type. + /// + /// The path of the file to be opened. + /// True if the operation was successful; otherwise, false. + private static bool OpenLogAssociated(string path) + { + try + { + // Open the file using the default application associated with its file type based on the user's preference + Process.Start(path); + return true; + } + catch + { + // If necessary, the error can be logged here. + return false; + } + } + + /// + /// Attempts to open a file in Notepad, the default text editor on Windows systems. + /// + /// The path of the file to be opened in Notepad. + /// True if the operation was successful; otherwise, false. + private static bool OpenLogNotepad(string path) + { + try + { + // Open it in "Notepad" (Windows default editor). + // Usually available on all Windows systems + Process.Start("notepad.exe", path); + return true; + } + catch + { + // If necessary, the error can be logged here. + return false; + } + } + + /// + /// Attempts to open the location of a specified file in Windows Explorer. + /// + /// The path of the file whose location needs to be opened. + /// True if the operation was successful; otherwise, false. + private static bool OpenLogLocation(string path) + { + try + { + /// when all fails open filelocation to logfile... + // Open Windows Explorer to the directory containing the file + Process.Start("explorer.exe", $"/select,\"{path}\""); + return true; + } + catch + { + // If necessary, the error can be logged here. + return false; + } + } + + #endregion } } \ No newline at end of file