2020-07-07 07:15:45 +12:00
|
|
|
|
// <copyright file="FileUrl.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
2019-07-05 05:04:14 +12:00
|
|
|
|
|
2020-03-28 12:42:32 +13:00
|
|
|
|
namespace SystemTrayMenu.Utilities
|
2019-07-05 05:04:14 +12:00
|
|
|
|
{
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using Microsoft.Win32;
|
|
|
|
|
|
2020-03-25 02:39:43 +13:00
|
|
|
|
public static class FileUrl
|
2019-07-05 05:04:14 +12:00
|
|
|
|
{
|
|
|
|
|
private static string browserPath = string.Empty;
|
2020-07-07 09:37:55 +12:00
|
|
|
|
|
2019-07-05 05:04:14 +12:00
|
|
|
|
public static string GetDefaultBrowserPath()
|
|
|
|
|
{
|
|
|
|
|
string urlAssociation = @"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http";
|
|
|
|
|
string browserPathKey = @"$BROWSER$\shell\open\command";
|
|
|
|
|
|
2020-07-07 07:15:45 +12:00
|
|
|
|
RegistryKey userChoiceKey;
|
2019-07-05 05:04:14 +12:00
|
|
|
|
string browserPath = FileUrl.browserPath;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(browserPath))
|
|
|
|
|
{
|
2020-07-07 09:37:55 +12:00
|
|
|
|
// Read default browser path from userChoiceLKey
|
2019-07-05 05:04:14 +12:00
|
|
|
|
userChoiceKey = Registry.CurrentUser.OpenSubKey(urlAssociation + @"\UserChoice", false);
|
|
|
|
|
|
2020-07-07 09:37:55 +12:00
|
|
|
|
// If user choice was not found, try machine default
|
2019-07-05 05:04:14 +12:00
|
|
|
|
if (userChoiceKey == null)
|
|
|
|
|
{
|
2020-07-07 09:37:55 +12:00
|
|
|
|
// Read default browser path from Win XP registry key
|
2020-03-24 00:38:23 +13:00
|
|
|
|
RegistryKey browserKey = Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command", false);
|
2019-07-05 05:04:14 +12:00
|
|
|
|
|
2020-07-07 09:37:55 +12:00
|
|
|
|
// If browser path wasn’t found, try Win Vista (and newer) registry key
|
2019-07-05 05:04:14 +12:00
|
|
|
|
if (browserKey == null)
|
|
|
|
|
{
|
|
|
|
|
browserKey =
|
|
|
|
|
Registry.CurrentUser.OpenSubKey(
|
|
|
|
|
urlAssociation, false);
|
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2020-03-24 00:38:23 +13:00
|
|
|
|
string path = CleanifyBrowserPath(browserKey.GetValue(null) as string);
|
2019-07-05 05:04:14 +12:00
|
|
|
|
browserKey.Close();
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// user defined browser choice was found
|
2020-07-07 07:15:45 +12:00
|
|
|
|
string progId = userChoiceKey.GetValue("ProgId").ToString();
|
2019-07-05 05:04:14 +12:00
|
|
|
|
userChoiceKey.Close();
|
|
|
|
|
|
|
|
|
|
// now look up the path of the executable
|
2020-07-04 08:02:16 +12:00
|
|
|
|
string concreteBrowserKey = browserPathKey.Replace("$BROWSER$", progId, System.StringComparison.InvariantCulture);
|
2020-03-24 00:38:23 +13:00
|
|
|
|
RegistryKey kp = Registry.ClassesRoot.OpenSubKey(concreteBrowserKey, false);
|
2019-07-05 05:04:14 +12:00
|
|
|
|
browserPath = CleanifyBrowserPath(kp.GetValue(null) as string);
|
|
|
|
|
kp.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileUrl.browserPath = browserPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return browserPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string CleanifyBrowserPath(string p)
|
|
|
|
|
{
|
|
|
|
|
string[] url = p.Split('"');
|
|
|
|
|
string clean = url[1];
|
|
|
|
|
return clean;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|