diff --git a/Business/Menus.cs b/Business/Menus.cs index 6a3164a..2c7c407 100644 --- a/Business/Menus.cs +++ b/Business/Menus.cs @@ -587,7 +587,7 @@ namespace SystemTrayMenu.Business private static void OpenFolder() { - Log.ProcessStart(Config.Path, null, true); + Log.ProcessStart(Config.Path); } private Menu Create(MenuData menuData, string title = null) diff --git a/Config/Config.cs b/Config/Config.cs index bbc1668..701a7f2 100644 --- a/Config/Config.cs +++ b/Config/Config.cs @@ -112,8 +112,7 @@ namespace SystemTrayMenu internal static void ShowHelpFAQ() { - string browserPath = FileUrl.GetDefaultBrowserPath(); - if (!string.IsNullOrEmpty(browserPath)) + if (FileUrl.GetDefaultBrowserPath(out string browserPath)) { Process.Start(browserPath, "https://github.com/Hofknecht/SystemTrayMenu#FAQ"); } diff --git a/DataClasses/RowData.cs b/DataClasses/RowData.cs index 1695d3e..32d3cd5 100644 --- a/DataClasses/RowData.cs +++ b/DataClasses/RowData.cs @@ -225,7 +225,7 @@ namespace SystemTrayMenu.DataClasses if (ContainsMenu && (e == null || e.Button == MouseButtons.Left)) { - Log.ProcessStart(TargetFilePath, null, true); + Log.ProcessStart(TargetFilePath); if (!Properties.Settings.Default.StaysOpenWhenItemClicked) { toCloseByDoubleClick = true; @@ -251,30 +251,10 @@ namespace SystemTrayMenu.DataClasses if (!ContainsMenu && (e == null || e.Button == MouseButtons.Left)) { - try + Log.ProcessStart(TargetFilePathOrig, arguments, true, workingDirectory, true); + if (!Properties.Settings.Default.StaysOpenWhenItemClicked) { - using Process p = new Process - { - StartInfo = new ProcessStartInfo(TargetFilePath) - { - FileName = TargetFilePathOrig, - Arguments = arguments, - WorkingDirectory = workingDirectory, - CreateNoWindow = true, - UseShellExecute = true, - }, - }; - p.Start(); - - if (!Properties.Settings.Default.StaysOpenWhenItemClicked) - { - toCloseByOpenItem = true; - } - } - catch (Win32Exception ex) - { - Log.Warn($"path:'{TargetFilePath}'", ex); - MessageBox.Show(ex.Message); + toCloseByOpenItem = true; } } } @@ -352,13 +332,7 @@ namespace SystemTrayMenu.DataClasses iconFile = file.Value("IconFile", string.Empty); if (string.IsNullOrEmpty(iconFile)) { - string browserPath = FileUrl.GetDefaultBrowserPath(); - if (string.IsNullOrEmpty(browserPath)) - { - Log.Info($"Resolve *.URL '{TargetFilePath}'" + - $"No default browser found!"); - } - else + if (FileUrl.GetDefaultBrowserPath(out string browserPath)) { icon = IconReader.GetFileIconWithCache(browserPath, true, out bool toDispose); diposeIcon = toDispose; diff --git a/Utilities/File/FileLnk.cs b/Utilities/File/FileLnk.cs index bd78bbb..2b48fd4 100644 --- a/Utilities/File/FileLnk.cs +++ b/Utilities/File/FileLnk.cs @@ -58,6 +58,38 @@ namespace SystemTrayMenu.Utilities !path.Substring(2).Contains(@"\", StringComparison.InvariantCulture); } + public static bool IsNetworkPath(string path) + { + return path.StartsWith(@"\\", StringComparison.InvariantCulture) && + !path.StartsWith(@"\\?\", StringComparison.InvariantCulture); + } + + public static bool PingHost(string nameOrAddress) + { + bool pingable = false; + Ping pinger = null; + + try + { + pinger = new Ping(); + PingReply reply = pinger.Send(nameOrAddress); + pingable = reply.Status == IPStatus.Success; + } + catch (PingException ex) + { + Log.Warn($"Ping {nameOrAddress} failed", ex); + } + finally + { + if (pinger != null) + { + pinger.Dispose(); + } + } + + return pingable; + } + private static string GetShortcutFileNamePath(object shortcutFilename) { string resolvedFilename = string.Empty; @@ -97,37 +129,5 @@ namespace SystemTrayMenu.Utilities return resolvedFilename; } - - public static bool IsNetworkPath(string path) - { - return path.StartsWith(@"\\", StringComparison.InvariantCulture) && - !path.StartsWith(@"\\?\", StringComparison.InvariantCulture); - } - - public static bool PingHost(string nameOrAddress) - { - bool pingable = false; - Ping pinger = null; - - try - { - pinger = new Ping(); - PingReply reply = pinger.Send(nameOrAddress); - pingable = reply.Status == IPStatus.Success; - } - catch (PingException ex) - { - Log.Warn($"Ping {nameOrAddress} failed", ex); - } - finally - { - if (pinger != null) - { - pinger.Dispose(); - } - } - - return pingable; - } } } \ No newline at end of file diff --git a/Utilities/File/FileUrl.cs b/Utilities/File/FileUrl.cs index 09ba292..2b4b194 100644 --- a/Utilities/File/FileUrl.cs +++ b/Utilities/File/FileUrl.cs @@ -10,13 +10,14 @@ namespace SystemTrayMenu.Utilities { private static string browserPath = string.Empty; - public static string GetDefaultBrowserPath() + public static bool GetDefaultBrowserPath(out string browserPath) { + bool valid = true; string urlAssociation = @"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http"; string browserPathKey = @"$BROWSER$\shell\open\command"; RegistryKey userChoiceKey; - string browserPath = FileUrl.browserPath; + browserPath = FileUrl.browserPath; if (string.IsNullOrEmpty(browserPath)) { @@ -39,7 +40,7 @@ namespace SystemTrayMenu.Utilities string path = CleanifyBrowserPath(browserKey.GetValue(null) as string); browserKey.Close(); - return path; + browserPath = path; } else { @@ -57,7 +58,13 @@ namespace SystemTrayMenu.Utilities FileUrl.browserPath = browserPath; } - return browserPath; + if (string.IsNullOrEmpty(browserPath)) + { + valid = false; + Log.Info($"No default browser found!"); + } + + return valid; } private static string CleanifyBrowserPath(string p) diff --git a/Utilities/Log.cs b/Utilities/Log.cs index f9b5ca2..0841f01 100644 --- a/Utilities/Log.cs +++ b/Utilities/Log.cs @@ -77,9 +77,14 @@ namespace SystemTrayMenu.Utilities Logger.ShutDown(); } - internal static void ProcessStart(string fileName, string arguments = null, bool doubleQuoteArg = false) + internal static void ProcessStart( + string fileName, + string arguments = "", + bool doubleQuoteArg = false, + string workingDirectory = "", + bool createNoWindow = false) { - if (doubleQuoteArg && arguments != null) + if (doubleQuoteArg && !string.IsNullOrEmpty(arguments)) { arguments = "\"" + arguments + "\""; } @@ -90,7 +95,10 @@ namespace SystemTrayMenu.Utilities { StartInfo = new ProcessStartInfo(fileName) { + FileName = fileName, Arguments = arguments, + WorkingDirectory = workingDirectory, + CreateNoWindow = createNoWindow, UseShellExecute = true, }, };