c# - 如何获取 C# 中当前运行时的信息?

标签 c# .net-4.0 .net-core

在 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 上使用。

归功于:

关于c# - 如何获取 C# 中当前运行时的信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43252446/

相关文章:

c# - 为什么 C# 的 String.Join 不在某一时刻使用 StringBuilder?

c# - ElasticSearch NEST API 将值更新为空

linq - ASP.NET MVC 2/.NET 4/Razor - 不能在 ViewModel 成员上使用 Any() 扩展方法

.net-core - .NET 核心 POSIX IPC

.net - 使用 .Net Core 运行 .Net Framework 应用程序(在 Linux 上)

C#托管代码引用类型问题

c# - C# 4.0 中的类属性/字段可以是匿名类型吗?

.net-4.0 - 派生类的c++/cli静态构造函数未被调用

c# - 在 .NET Core/.NET 5+ 中使用 TransactionScope 和 SQL 连接池时如何避免 PlatformNotSupportedException

c# - Silverlight 独立存储最佳实践?