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

查看:41
本文介绍了使用 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 Standard (1.2).问题是我不确定 .NET Standard 与 RegistryKeyProcess(和 ProcessStartInfo)的等价物是什么.当然,对于其他平台,该系统上可能没有注册表或任何进程"概念.那么有没有办法在 .NET Standard 中实现上述功能呢?(或者,这可能是一个 X-Y 问题,而我的做法全错了?)

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

推荐答案

Registry 仅在使用 Microsoft.Win32.Registry NuGet 包的 .NET Standard 1.3 中可用.因此,如果您想使用注册表,则不能以 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 可用于使用 NuGet 包 System.Diagnostics.Process 的 .NET Standard 1.3(对于 .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天全站免登陆