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

Abi*_*n47 7 .net c# registry process .net-standard

我有一个来自现有.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);
Run Code Online (Sandbox Code Playgroud)

这个项目试图让设置跨平台,所以我将它移植到.NET Standard(1.2).问题是我不确定.NET标准的等价物RegistryKeyProcess(和ProcessStartInfo).当然,对于其他平台,可能没有注册表或该系统上的任何"进程"概念.那么有没有办法在.NET Standard中实现上述功能?(或者这可能是一个XY问题而且我错了?)

Mar*_*ich 8

Registry仅在使用Microsoft.Win32.RegistryNuGet包的.NET Standard 1.3中提供.因此,如果要使用注册表,则无法定位1.2.

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

为了获得"默认浏览器",没有完整的跨平台解决方案,因为您需要与用户正在使用的任何桌面解决方案集成 - 例如MacOS,KDE,GNOME,Ubuntu Unity等.