Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

As a library, target netstandard, not netcoreapp #1

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions MintPlayer.PlatformBrowser/MintPlayer.PlatformBrowser.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Authors>Pieterjan De Clippel</Authors>
<Company>MintPlayer</Company>
<Description>This package allows you to retrieve the web browsers (including Microsoft Edge) installed on the system.</Description>
Expand All @@ -14,14 +13,15 @@

<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Enums\eAssociationType.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Win32.Registry" Version="4.6.0" />
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
</ItemGroup>

</Project>
176 changes: 86 additions & 90 deletions MintPlayer.PlatformBrowser/PlatformBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics;
using MintPlayer.PlatformBrowser.Exceptions;
using System.Globalization;
using System.IO;

namespace MintPlayer.PlatformBrowser
{
Expand All @@ -16,121 +17,112 @@ public static ReadOnlyCollection<Browser> GetInstalledBrowsers()
{
#region Get registry key containing browser information

var internetKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\Clients\StartMenuInternet");
if (internetKey == null)
{
internetKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Clients\StartMenuInternet");
}
var internetKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\Clients\StartMenuInternet") ??
Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Clients\StartMenuInternet");

#endregion

#region Loop through keys

var result = new List<Browser>();
foreach (var browserName in internetKey.GetSubKeyNames())
{
try
if (internetKey != null)
foreach (var browserName in internetKey.GetSubKeyNames())
{
// Key containing browser information
var browserKey = internetKey.OpenSubKey(browserName);
try
{
// Key containing browser information
var browserKey = internetKey.OpenSubKey(browserName);

// Key containing executable path
var commandKey = browserKey.OpenSubKey(@"shell\open\command");
// Key containing executable path
var commandKey = browserKey.OpenSubKey(@"shell\open\command");

// Key containing icon path
var iconKey = browserKey.OpenSubKey(@"DefaultIcon");
var iconPath = (string)iconKey.GetValue(null);
var iconParts = iconPath.Split(',');
// Key containing icon path
var iconKey = browserKey.OpenSubKey(@"DefaultIcon");
var iconPath = (string) iconKey.GetValue(null);
var iconParts = iconPath.Split(',');

// Validate the values
string executablePath = ((string)commandKey.GetValue(null)).Trim('"');
// Validate the values
string executablePath = ((string) commandKey.GetValue(null)).Trim('"');

// ExecutablePath must be .exe
if (System.IO.Path.GetExtension(executablePath) != ".exe")
{
throw new BrowserException("ExecutablePath must be .exe");
}
// ExecutablePath must be .exe
if (Path.GetExtension(executablePath) != ".exe")
{
throw new BrowserException("ExecutablePath must be .exe");
}

// IconPath must be .exe or .ico
bool iconValid = new[] {".exe", ".ico"}.Contains(Path.GetExtension(iconParts[0]));

// IconPath must be .exe or .ico
var iconValid = true;
if (!new[] { ".exe", ".ico" }.Contains(System.IO.Path.GetExtension(iconParts[0])))
{
iconValid = false;
}

ReadOnlyDictionary<string, object> fileAssociations;
ReadOnlyDictionary<string, object> urlAssociations;
try
{
// Read the FileAssociations
var fileAssociationsKey = browserKey.OpenSubKey(@"Capabilities\FileAssociations");
fileAssociations = new ReadOnlyDictionary<string, object>(fileAssociationsKey.GetValueNames().ToDictionary(v => v, v => fileAssociationsKey.GetValue(v)));
}
catch (Exception)
{
#region Disconfigured browser installation, Add more browsers to this list

string browserIdFormat = string.Empty;
if (browserName.Equals("IEXPLORE.EXE"))
{
browserIdFormat = "IE.AssocFile.{0}";
}

#endregion

// Add a default list of file associations
var associations = CreateDefaultFileAssociations(browserIdFormat);

fileAssociations = new ReadOnlyDictionary<string, object>(associations);
}

try
{
ReadOnlyDictionary<string, object> fileAssociations = new ReadOnlyDictionary<string, object>(associations);
// Read the FileAssociations
var fileAssociationsKey = browserKey.OpenSubKey(@"Capabilities\FileAssociations");
if (fileAssociationsKey != null)
{
fileAssociations = new ReadOnlyDictionary<string, object>(fileAssociationsKey
.GetValueNames().ToDictionary(v => v, v => fileAssociationsKey.GetValue(v)));
}
// Use empty list for UrlAssociations
var urlAssociations =
new ReadOnlyDictionary<string, object>(new Dictionary<string, object>());
// Read the UrlAssociations
var urlAssociationsKey = browserKey.OpenSubKey(@"Capabilities\URLAssociations");
urlAssociations = new ReadOnlyDictionary<string, object>(urlAssociationsKey.GetValueNames().ToDictionary(v => v, v => urlAssociationsKey.GetValue(v)));
if (urlAssociationsKey != null)
{
urlAssociations = new ReadOnlyDictionary<string, object>(urlAssociationsKey
.GetValueNames()
.ToDictionary(v => v, v => urlAssociationsKey.GetValue(v)));
}

var browser = new Browser
{
KeyName = browserName,
Name = (string) browserKey.GetValue(null),
ExecutablePath = executablePath,
Version = FileVersionInfo.GetVersionInfo(executablePath),
IconPath = iconValid ? iconParts[0] : executablePath,
IconIndex = !iconValid
? 0
: iconParts.Length > 1
? Convert.ToInt32(iconParts[1])
: 0,
FileAssociations = fileAssociations,
UrlAssociations = urlAssociations
};
result.Add(browser);
}
catch (Exception)
{
// Use empty list for UrlAssociations
urlAssociations = new ReadOnlyDictionary<string, object>(new Dictionary<string, object>());
// Disconfigured browser
}


var browser = new Browser
{
KeyName = browserName,
Name = (string)browserKey.GetValue(null),
ExecutablePath = executablePath,
Version = FileVersionInfo.GetVersionInfo(executablePath),
IconPath = iconValid ? iconParts[0] : executablePath,
IconIndex = !iconValid
? 0
: iconParts.Length > 1
? Convert.ToInt32(iconParts[1])
: 0,
FileAssociations = fileAssociations,
UrlAssociations = urlAssociations
};
result.Add(browser);
}
catch (Exception)
{
// Disconfigured browser
}
}

#endregion

#region Check if Edge is installed

var systemAppsFolder = @"C:\Windows\SystemApps\";
if (System.IO.Directory.Exists(systemAppsFolder))
if (Directory.Exists(systemAppsFolder))
{
string[] directories = System.IO.Directory.GetDirectories(systemAppsFolder);
string[] directories = Directory.GetDirectories(systemAppsFolder);
var edgeFolder = directories.FirstOrDefault(d => d.StartsWith($"{systemAppsFolder}Microsoft.MicrosoftEdge_"));

if (edgeFolder != null)
{
if (System.IO.File.Exists($@"{edgeFolder}\MicrosoftEdge.exe"))
if (File.Exists($@"{edgeFolder}\MicrosoftEdge.exe"))
{
var edgePath = $@"{edgeFolder}\MicrosoftEdge.exe";
result.Add(new Browser
Expand All @@ -157,16 +149,17 @@ public static ReadOnlyCollection<Browser> GetInstalledBrowsers()

private static Dictionary<string, object> CreateDefaultFileAssociations(string browserIdFormat)
{
var associations = new Dictionary<string, object>(new[] {
new KeyValuePair<string, object>(".htm", string.Format(browserIdFormat, "HTM")),
new KeyValuePair<string, object>(".html", string.Format(browserIdFormat, "HTML")),
new KeyValuePair<string, object>(".pdf", string.Format(browserIdFormat, "PDF")),
new KeyValuePair<string, object>(".shtml", string.Format(browserIdFormat, "SHTML")),
new KeyValuePair<string, object>(".svg", string.Format(browserIdFormat, "SVG")),
new KeyValuePair<string, object>(".webp", string.Format(browserIdFormat, "WEBP")),
new KeyValuePair<string, object>(".xht", string.Format(browserIdFormat, "XHT")),
new KeyValuePair<string, object>(".xhtml", string.Format(browserIdFormat, "XHTML"))
});
var associations = new Dictionary<string, object>
{
{".htm", string.Format(browserIdFormat, "HTM")},
{".html", string.Format(browserIdFormat, "HTML")},
{".pdf", string.Format(browserIdFormat, "PDF")},
{".shtml", string.Format(browserIdFormat, "SHTML")},
{".svg", string.Format(browserIdFormat, "SVG")},
{".webp", string.Format(browserIdFormat, "WEBP")},
{".xht", string.Format(browserIdFormat, "XHT")},
{".xhtml", string.Format(browserIdFormat, "XHTML")}
};
return associations;
}

Expand All @@ -177,14 +170,14 @@ private static Dictionary<string, object> CreateDefaultFileAssociations(string b
public static Browser GetDefaultBrowser(IEnumerable<Browser> browsers, Enums.eProtocolType protocolType)
{
var urlAssociationsKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\Shell\Associations\URLAssociations");
var protocolName = Enum.GetName(typeof(Enums.eProtocolType), protocolType).ToLower(CultureInfo.InvariantCulture);
if (!urlAssociationsKey.GetSubKeyNames().Contains(protocolName))
var protocolName = Enum.GetName(typeof(Enums.eProtocolType), protocolType)?.ToLower(CultureInfo.InvariantCulture);
if (urlAssociationsKey != null && !urlAssociationsKey.GetSubKeyNames().Contains(protocolName))
{
throw new BrowserException($"No url association for {protocolName}");
}

var userChoiceKey = urlAssociationsKey.OpenSubKey($@"{protocolName}\UserChoice");
var defaultBrowserProgId = userChoiceKey.GetValue("ProgId");
var userChoiceKey = urlAssociationsKey?.OpenSubKey($@"{protocolName}\UserChoice");
var defaultBrowserProgId = userChoiceKey?.GetValue("ProgId");

switch (defaultBrowserProgId)
{
Expand Down Expand Up @@ -232,16 +225,19 @@ public static Browser GetDefaultBrowser()
/// <param name="fileType">The filetype you want to open</param>
public static Browser GetDefaultBrowser(IEnumerable<Browser> browsers, Enums.eFileType fileType)
{
var ext = Enum.GetName(typeof(Enums.eFileType), fileType).ToLower(CultureInfo.InvariantCulture);
var ext = Enum.GetName(typeof(Enums.eFileType), fileType)?.ToLower(CultureInfo.InvariantCulture);
var fileExtsKey = Registry.CurrentUser.OpenSubKey($@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts");
var subkeyNames = fileExtsKey.GetSubKeyNames();

if (!subkeyNames.Contains($@".{ext}"))
if (fileExtsKey == null || !fileExtsKey.GetSubKeyNames().Contains($@".{ext}"))
{
throw new BrowserException("The specified filetype is not present in the registry");
}

var fileTypeKey = fileExtsKey.OpenSubKey($@".{ext}\UserChoice");
if (fileTypeKey == null)
{
throw new BrowserException("The specified filetype is not present in the registry");
}
var progId = fileTypeKey.GetValue("ProgId");

return browsers.FirstOrDefault(
Expand Down