Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Porting .NET Framework code to .NET Standard, using Process, RegistryKey

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?)

like image 800
Abion47 Avatar asked May 24 '17 07:05

Abion47


2 Answers

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.

like image 138
Martin Ullrich Avatar answered Sep 28 '22 04:09

Martin Ullrich


For registry at least there is a solution available in .NET Standard, but it is only compatible from version 1.3:

The Windows registry is a self-contained component that will be provided as a separate NuGet package (e.g. Microsoft.Win32.Registry). You’ll be able to consume it from .NET Core, but it will only work on Windows. Calling registry APIs from any other OS will result in PlatformNotSupportedException. You’re expected to guard your calls appropriately or making sure your code will only ever run on Windows. We’re considering improving our tooling to help you with detecting these cases.

So include the appropriate NuGet package and you should be fine for the Registry class.

System.Diagnostics.Process is supported in .NET Standard 1.3, so that should be fine too if you can migrate.

That said, I don't think you will get this part of the program working this way, for Windows, yes, but for other platform you might need an entire different approach.

like image 21
Patrick Hofman Avatar answered Sep 28 '22 06:09

Patrick Hofman