Merge pull request #2596 from xRushG/OpenLogBug

Log File Opening Fails on Notification Settings Page
This commit is contained in:
Dimitrij
2024-05-24 09:12:38 +01:00
committed by GitHub
2 changed files with 81 additions and 2 deletions

View File

@@ -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

View File

@@ -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
/// <summary>
/// Attempts to open a file using the default application associated with its file type.
/// </summary>
/// <param name="path">The path of the file to be opened.</param>
/// <returns>True if the operation was successful; otherwise, false.</returns>
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;
}
}
/// <summary>
/// Attempts to open a file in Notepad, the default text editor on Windows systems.
/// </summary>
/// <param name="path">The path of the file to be opened in Notepad.</param>
/// <returns>True if the operation was successful; otherwise, false.</returns>
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;
}
}
/// <summary>
/// Attempts to open the location of a specified file in Windows Explorer.
/// </summary>
/// <param name="path">The path of the file whose location needs to be opened.</param>
/// <returns>True if the operation was successful; otherwise, false.</returns>
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
}
}