使用 Process、RegistryKey 将 .NET Framework 代码移植到 .NET Standard

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

我有一个来自现有 .NET Framework 项目的方法,它从注册表中获取 Windows 中默认浏览器的路径,并通过 Process 调用启动它:

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

此项目正在尝试进行跨平台设置,因此我将其移植到 .NET Standard (1.2)。问题是我不确定 RegistryKeyProcess(以及 ProcessStartInfo)的 .NET Standard 等价物是什么。当然,对于其他平台,该系统上可能没有注册表或任何 "processes" 的概念。那么有没有办法在.NET Standard中实现上述功能呢? (或者这可能是一个 X-Y 问题,而我做错了?)

注册表至少有一个 solution available in .NET Standard,但它只兼容 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.

因此,请包含适当的 NuGet 包,您应该适合 Registry class。

System.Diagnostics.Processis supported in .NET Standard 1.3,能迁移的话应该也可以吧

就是说,我认为您不会让程序的这一部分以这种方式工作,对于 Windows,是的,但是对于其他平台,您可能需要一种完全不同的方法。

注册表仅在使用 Microsoft.Win32.Registry NuGet 包的 .NET Standard 1.3 中可用。因此,如果您想使用注册表,则不能针对 1.2。

同样,System.Diagnostics.Process 可用于使用 NuGet 包的 .NET Standard 1.3 System.Diagnostics.Process(对于 .NET Standard 2.0,不需要单独的 NuGet 包来访问它)。

为了获得 "default browser",没有完整的跨平台解决方案,因为您需要与用户使用的任何桌面解决方案集成 - 例如MacOS、KDE、GNOME、Ubuntu Unity 等