使用Process,RegistryKey将.NET Framework代码移植到.NET Standard [英] Porting .NET Framework code to .NET Standard, using Process, RegistryKey

查看:119
本文介绍了使用Process,RegistryKey将.NET Framework代码移植到.NET Standard的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

该项目试图使安装程序跨平台运行,因此我将其移植到。 NET标准(1.2)。问题是,我不确定<.c.c.c.net标准等效于 RegistryKey Process (和 ProcessStartInfo )。当然,对于其他平台,该系统上可能没有注册表或进程的任何概念。那么有没有办法在.NET Standard中实现上述功能? (或者这可能是XY问题,而我要解决所有问题吗?)

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

推荐答案

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

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.

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

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

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

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.

这篇关于使用Process,RegistryKey将.NET Framework代码移植到.NET Standard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆