From c38c8e9b43763ecc009a4cc95a5a59dfbd42473d Mon Sep 17 00:00:00 2001 From: Merijn de Jonge Date: Wed, 27 Jan 2021 15:22:59 +0100 Subject: [PATCH 1/3] Support netstandard2.0 and netstandard2.1 --- .../MintPlayer.PlatformBrowser.csproj | 2 +- MintPlayer.PlatformBrowser/PlatformBrowser.cs | 21 ++++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/MintPlayer.PlatformBrowser/MintPlayer.PlatformBrowser.csproj b/MintPlayer.PlatformBrowser/MintPlayer.PlatformBrowser.csproj index 1c5a8b9..decafcf 100644 --- a/MintPlayer.PlatformBrowser/MintPlayer.PlatformBrowser.csproj +++ b/MintPlayer.PlatformBrowser/MintPlayer.PlatformBrowser.csproj @@ -1,7 +1,6 @@  - netcoreapp3.1 Pieterjan De Clippel MintPlayer This package allows you to retrieve the web browsers (including Microsoft Edge) installed on the system. @@ -14,6 +13,7 @@ true snupkg + netstandard2.0;netstandard2.1 diff --git a/MintPlayer.PlatformBrowser/PlatformBrowser.cs b/MintPlayer.PlatformBrowser/PlatformBrowser.cs index 0730eca..3184370 100644 --- a/MintPlayer.PlatformBrowser/PlatformBrowser.cs +++ b/MintPlayer.PlatformBrowser/PlatformBrowser.cs @@ -157,16 +157,17 @@ public static ReadOnlyCollection GetInstalledBrowsers() private static Dictionary CreateDefaultFileAssociations(string browserIdFormat) { - var associations = new Dictionary(new[] { - new KeyValuePair(".htm", string.Format(browserIdFormat, "HTM")), - new KeyValuePair(".html", string.Format(browserIdFormat, "HTML")), - new KeyValuePair(".pdf", string.Format(browserIdFormat, "PDF")), - new KeyValuePair(".shtml", string.Format(browserIdFormat, "SHTML")), - new KeyValuePair(".svg", string.Format(browserIdFormat, "SVG")), - new KeyValuePair(".webp", string.Format(browserIdFormat, "WEBP")), - new KeyValuePair(".xht", string.Format(browserIdFormat, "XHT")), - new KeyValuePair(".xhtml", string.Format(browserIdFormat, "XHTML")) - }); + var associations = new Dictionary + { + {".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; } From 24863debf8d0fc44270793a3ac054024f8531381 Mon Sep 17 00:00:00 2001 From: Merijn de Jonge Date: Wed, 27 Jan 2021 15:23:48 +0100 Subject: [PATCH 2/3] Fix ReSharper warnings that could cause null pointer exceptions --- MintPlayer.PlatformBrowser/PlatformBrowser.cs | 178 +++++++++--------- 1 file changed, 90 insertions(+), 88 deletions(-) diff --git a/MintPlayer.PlatformBrowser/PlatformBrowser.cs b/MintPlayer.PlatformBrowser/PlatformBrowser.cs index 3184370..b45786a 100644 --- a/MintPlayer.PlatformBrowser/PlatformBrowser.cs +++ b/MintPlayer.PlatformBrowser/PlatformBrowser.cs @@ -6,6 +6,7 @@ using System.Diagnostics; using MintPlayer.PlatformBrowser.Exceptions; using System.Globalization; +using System.IO; namespace MintPlayer.PlatformBrowser { @@ -16,121 +17,119 @@ public static ReadOnlyCollection 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(); - 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 - var iconValid = true; - if (!new[] { ".exe", ".ico" }.Contains(System.IO.Path.GetExtension(iconParts[0]))) - { - iconValid = false; - } + // IconPath must be .exe or .ico + bool iconValid = new[] {".exe", ".ico"}.Contains(Path.GetExtension(iconParts[0])); - ReadOnlyDictionary fileAssociations; - ReadOnlyDictionary urlAssociations; - try - { - // Read the FileAssociations - var fileAssociationsKey = browserKey.OpenSubKey(@"Capabilities\FileAssociations"); - fileAssociations = new ReadOnlyDictionary(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")) + ReadOnlyDictionary fileAssociations; + ReadOnlyDictionary urlAssociations; + try { - browserIdFormat = "IE.AssocFile.{0}"; + // Read the FileAssociations + var fileAssociationsKey = browserKey.OpenSubKey(@"Capabilities\FileAssociations"); + fileAssociations = new ReadOnlyDictionary(fileAssociationsKey + .GetValueNames().ToDictionary(v => v, v => fileAssociationsKey.GetValue(v))); } - #endregion + catch (Exception) + { + #region Disconfigured browser installation, Add more browsers to this list - // Add a default list of file associations - var associations = CreateDefaultFileAssociations(browserIdFormat); + string browserIdFormat = string.Empty; + if (browserName.Equals("IEXPLORE.EXE")) + { + browserIdFormat = "IE.AssocFile.{0}"; + } - fileAssociations = new ReadOnlyDictionary(associations); - } + #endregion - try - { - // Read the UrlAssociations - var urlAssociationsKey = browserKey.OpenSubKey(@"Capabilities\URLAssociations"); - urlAssociations = new ReadOnlyDictionary(urlAssociationsKey.GetValueNames().ToDictionary(v => v, v => urlAssociationsKey.GetValue(v))); + // Add a default list of file associations + var associations = CreateDefaultFileAssociations(browserIdFormat); + + fileAssociations = new ReadOnlyDictionary(associations); + } + + try + { + // Read the UrlAssociations + var urlAssociationsKey = browserKey.OpenSubKey(@"Capabilities\URLAssociations"); + urlAssociations = new ReadOnlyDictionary(urlAssociationsKey.GetValueNames() + .ToDictionary(v => v, v => urlAssociationsKey.GetValue(v))); + } + catch (Exception) + { + // Use empty list for UrlAssociations + urlAssociations = new ReadOnlyDictionary(new Dictionary()); + } + + + 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(new Dictionary()); + // 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 @@ -178,14 +177,14 @@ private static Dictionary CreateDefaultFileAssociations(string b public static Browser GetDefaultBrowser(IEnumerable 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) { @@ -233,16 +232,19 @@ public static Browser GetDefaultBrowser() /// The filetype you want to open public static Browser GetDefaultBrowser(IEnumerable 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( From 68a851ea22cfe49d0f1e2bcb23fabc73ca482ec2 Mon Sep 17 00:00:00 2001 From: Merijn de Jonge Date: Fri, 5 Feb 2021 15:58:54 +0100 Subject: [PATCH 3/3] Refactor to remove exceptions as part of happy flow --- .../MintPlayer.PlatformBrowser.csproj | 2 +- MintPlayer.PlatformBrowser/PlatformBrowser.cs | 55 ++++++++----------- 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/MintPlayer.PlatformBrowser/MintPlayer.PlatformBrowser.csproj b/MintPlayer.PlatformBrowser/MintPlayer.PlatformBrowser.csproj index decafcf..c6349b5 100644 --- a/MintPlayer.PlatformBrowser/MintPlayer.PlatformBrowser.csproj +++ b/MintPlayer.PlatformBrowser/MintPlayer.PlatformBrowser.csproj @@ -21,7 +21,7 @@ - + diff --git a/MintPlayer.PlatformBrowser/PlatformBrowser.cs b/MintPlayer.PlatformBrowser/PlatformBrowser.cs index b45786a..f2d59c5 100644 --- a/MintPlayer.PlatformBrowser/PlatformBrowser.cs +++ b/MintPlayer.PlatformBrowser/PlatformBrowser.cs @@ -53,47 +53,40 @@ public static ReadOnlyCollection GetInstalledBrowsers() // IconPath must be .exe or .ico bool iconValid = new[] {".exe", ".ico"}.Contains(Path.GetExtension(iconParts[0])); - ReadOnlyDictionary fileAssociations; - ReadOnlyDictionary urlAssociations; - try + + #region Disconfigured browser installation, Add more browsers to this list + + string browserIdFormat = string.Empty; + if (browserName.Equals("IEXPLORE.EXE")) { - // Read the FileAssociations - var fileAssociationsKey = browserKey.OpenSubKey(@"Capabilities\FileAssociations"); - fileAssociations = new ReadOnlyDictionary(fileAssociationsKey - .GetValueNames().ToDictionary(v => v, v => fileAssociationsKey.GetValue(v))); + browserIdFormat = "IE.AssocFile.{0}"; } - 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 + #endregion - // Add a default list of file associations - var associations = CreateDefaultFileAssociations(browserIdFormat); + // Add a default list of file associations + var associations = CreateDefaultFileAssociations(browserIdFormat); - fileAssociations = new ReadOnlyDictionary(associations); - } - - try + ReadOnlyDictionary fileAssociations = new ReadOnlyDictionary(associations); + // Read the FileAssociations + var fileAssociationsKey = browserKey.OpenSubKey(@"Capabilities\FileAssociations"); + if (fileAssociationsKey != null) { - // Read the UrlAssociations - var urlAssociationsKey = browserKey.OpenSubKey(@"Capabilities\URLAssociations"); - urlAssociations = new ReadOnlyDictionary(urlAssociationsKey.GetValueNames() - .ToDictionary(v => v, v => urlAssociationsKey.GetValue(v))); + fileAssociations = new ReadOnlyDictionary(fileAssociationsKey + .GetValueNames().ToDictionary(v => v, v => fileAssociationsKey.GetValue(v))); } - catch (Exception) + // Use empty list for UrlAssociations + var urlAssociations = + new ReadOnlyDictionary(new Dictionary()); + // Read the UrlAssociations + var urlAssociationsKey = browserKey.OpenSubKey(@"Capabilities\URLAssociations"); + if (urlAssociationsKey != null) { - // Use empty list for UrlAssociations - urlAssociations = new ReadOnlyDictionary(new Dictionary()); + urlAssociations = new ReadOnlyDictionary(urlAssociationsKey + .GetValueNames() + .ToDictionary(v => v, v => urlAssociationsKey.GetValue(v))); } - var browser = new Browser { KeyName = browserName,