[Feature] Refactor GetDefaultBrowserPath, ProcessStart (#169), version 1.0.17.31

This commit is contained in:
Markus Hofknecht 2021-05-02 15:01:31 +02:00
parent 7301b61a42
commit c12ed3fc4e
6 changed files with 60 additions and 72 deletions

View file

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

View file

@ -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");
}

View file

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

View file

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

View file

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

View file

@ -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,
},
};