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

编程入门 行业动态 更新时间:2024-10-23 17:23:50
本文介绍了使用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标准等效于 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

本文发布于:2023-11-15 19:06:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1597167.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   RegistryKey   Process   NET   Standard

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!