I have a method from an existing .NET Framework project that gets the path to the default browser in Windows from the Registry and launches it with a Process
call:
string browser = "";
RegistryKey regKey = null;
try
{
regKey = Registry.ClassesRoot.OpenSubKey("HTTP\\shell\\open\\command", false);
browser = regKey.GetValue(null).ToString().Replace("" + (char)34, "");
if (!browser.EndsWith(".exe"))
browser = browser.Substring(0, browser.LastIndexOf(".exe") + 4);
}
finally
{
if (regKey != null)
regKey.Close();
}
ProcessStartInfo info = new ProcessStartInfo(browser);
info.Arguments = "\"" + url + "\"";
Process.Start(info);
This project is trying to get setup to go cross-platform, so I'm porting it to .NET Standard (1.2). The problem is that I'm not sure what the .NET Standard equivalents are to RegistryKey
and Process
(and ProcessStartInfo
). Of course, being for other platforms, there may not be a registry or any concept of "processes" on that system. So is there a way to achieve the above functionality in .NET Standard? (Or perhaps this is an X-Y Problem and I'm going about it all wrong?)
Registry is only available in .NET Standard 1.3 using the Microsoft.Win32.Registry
NuGet package. So you cannot target 1.2 if you want to use the registry.
Similarly, System.Diagnostics.Process
is available for .NET Standard 1.3 using the NuGet package System.Diagnostics.Process
(for .NET Standard 2.0, no separate NuGet package is required to access it).
For getting the "default browser", there is not full cross-platform solution since you'd need to integrate with whatever desktop solution the user is using - e.g. MacOS, KDE, GNOME, Ubuntu Unity etc.