在 C# 程序中,我需要获取有关程序运行的运行时环境的信息。
本质上,我需要知道当前程序是在 .NET Core 还是完整的 .NET Framework 4.x 中运行。
像下面这样的东西可能会起作用:
public string GetRuntimeVersion()
{
#if NET451
return "net451";
#elseif netcoreapp11
return "netcoreapp11";
#elseif netstandard14
return "netcoreapp14";
#endif
...
}
但是有更好的方法吗?
最佳答案
Microsoft.Extensions.PlatformAbstractions !
using Microsoft.Extensions.PlatformAbstractions;
var runtimeInfo = PlatformServices.Default.Application.RuntimeFramework;
PlatformServices.Default.Application.RuntimeFramework
属性包含运行时标识符及其版本等信息。并且可在 .net core 上使用。
归功于:
Jurjen感谢指出
System.Environment.Version
,它以某种方式引导我到 Victor Hurdugaci的评论
关于c# - 如何获取 C# 中当前运行时的信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43252446/