SystemTrayMenu/Utilities/File/FileUrl.cs

84 lines
3 KiB
C#
Raw Normal View History

// <copyright file="FileUrl.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
2019-07-05 05:04:14 +12:00
namespace SystemTrayMenu.Utilities
2019-07-05 05:04:14 +12:00
{
using Microsoft.Win32;
public static class FileUrl
2019-07-05 05:04:14 +12:00
{
private static string browserPath = string.Empty;
public static bool GetDefaultBrowserPath(out string browserPath)
2019-07-05 05:04:14 +12:00
{
bool valid = true;
2019-07-05 05:04:14 +12:00
string urlAssociation = @"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http";
string browserPathKey = @"$BROWSER$\shell\open\command";
RegistryKey userChoiceKey;
browserPath = FileUrl.browserPath;
2019-07-05 05:04:14 +12:00
if (string.IsNullOrEmpty(browserPath))
{
// Read default browser path from userChoiceLKey
2019-07-05 05:04:14 +12:00
userChoiceKey = Registry.CurrentUser.OpenSubKey(urlAssociation + @"\UserChoice", false);
// If user choice was not found, try machine default
2019-07-05 05:04:14 +12:00
if (userChoiceKey == null)
{
// Read default browser path from Win XP registry key
RegistryKey browserKey = Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command", false);
2019-07-05 05:04:14 +12:00
// If browser path wasnt 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);
}
if (browserKey != null)
{
string path = CleanifyBrowserPath(browserKey.GetValue(null) as string);
browserKey.Close();
browserPath = path;
}
2019-07-05 05:04:14 +12:00
}
else
{
// user defined browser choice was found
string progId = userChoiceKey.GetValue("ProgId").ToString();
2019-07-05 05:04:14 +12:00
userChoiceKey.Close();
// now look up the path of the executable
string concreteBrowserKey = browserPathKey.Replace("$BROWSER$", progId, System.StringComparison.InvariantCulture);
RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(concreteBrowserKey, false);
if (registryKey != null)
{
browserPath = CleanifyBrowserPath(registryKey.GetValue(null) as string);
registryKey.Close();
}
2019-07-05 05:04:14 +12:00
}
FileUrl.browserPath = browserPath;
}
if (string.IsNullOrEmpty(browserPath))
{
valid = false;
Log.Info($"No default browser found!");
}
return valid;
2019-07-05 05:04:14 +12:00
}
private static string CleanifyBrowserPath(string p)
{
string[] url = p.Split('"');
string clean = url[1];
return clean;
}
}
}