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 starts 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 set up a cross-platform, so I am porting it to .NET Standard (1.2). The problem is that I'm not sure if the .NET Standard equivalents correspond to RegistryKey and Process (and ProcessStartInfo ). Of course, for other platforms there can be no registry or any concept of โ€œprocessesโ€ in this system. So, is there a way to achieve the above functionality in .NET Standard? (Or maybe this is an XY problem and will I fix it?)

+5
source share
2 answers

The registry is only available in .NET Standard 1.3 using the Microsoft.Win32.Registry NuGet package. Thus, 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, a separate NuGet package is not required to access it).

There is no complete cross-platform solution to get a โ€œdefault browser,โ€ since you will need to integrate with any desktop solution that the user uses โ€” for example, MacOS, KDE, GNOME, Ubuntu Unity, etc.

+6
source

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

The Windows registry is a standalone component that will be provided as a separate NuGet package (for example, Microsoft.Win32.Registry ). You can use it with .NET Core, but it will only work on Windows. Calling the registry APIs from any other OS will result in a PlatformNotSupportedException. It is expected that you will protect your calls accordingly or make sure that your code will only work on Windows. We have been considering improving our tools to help you detect these cases.

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

System.Diagnostics.Process supported in .NET Standard 1.3 , so this is also good if you can perform the migration.

However, I donโ€™t think that you will get this part of the program that works just like that for Windows, yes, but for a different platform you may need a completely different approach.

+1
source

Source: https://habr.com/ru/post/1268160/


All Articles