如何在 C# 中获取有关当前运行时的信息?
How to get info about the current runtime in C#?
在 C# 程序中,我需要获取有关程序所在的运行时环境的信息 运行。
本质上,我需要知道当前程序是 运行 in .NET Core 还是 Full .NET Framework 4.x。
类似以下的方法可能有效:
public string GetRuntimeVersion()
{
#if NET451
return "net451";
#elseif netcoreapp11
return "netcoreapp11";
#elseif netstandard14
return "netcoreapp14";
#endif
...
}
但是有没有更好的方法呢?
勾选 System.Environment.Version
属性 (http://msdn.microsoft.com/en-us/library/system.environment.version.aspx).
public static void Main()
{
Console.WriteLine("Version: {0}", Environment.Version.ToString());
}
Microsoft.Extensions.PlatformAbstractions !
using Microsoft.Extensions.PlatformAbstractions;
var runtimeInfo = PlatformServices.Default.Application.RuntimeFramework;
PlatformServices.Default.Application.RuntimeFramework
属性 包含运行时标识符及其版本等信息。并且在 .net 核心上可用。
致谢:
Victor Hurdugaci for
Jurjen for pointing out System.Environment.Version
which somehow led me to Victor Hurdugaci的评论
现在好像还有System.Runtime.InteropServices.RuntimeInformation
。
PROPERTIES
FrameworkDescription
Gets the name of the .NET installation on which an app is running.
OSArchitecture
Gets the platform architecture on which the current app is running.
OSDescription
Gets a string that describes the operating system on which the app is running.
ProcessArchitecture
Gets the process architecture of the currently running app.
RuntimeIdentifier
Gets the platform on which an app is running.
using System;
public class Program
{
public static void Main()
{
Console.WriteLine(System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription);
Console.WriteLine(System.Runtime.InteropServices.RuntimeInformation.OSArchitecture);
Console.WriteLine(System.Runtime.InteropServices.RuntimeInformation.OSDescription);
Console.WriteLine(System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture);
Console.WriteLine(System.Runtime.InteropServices.RuntimeInformation.RuntimeIdentifier);
}
}
.NET 6.0.0-rtm.21522.10
X64
Linux 5.4.0-1064-azure #67~18.04.1-Ubuntu SMP Wed Nov 10 11:38:21 UTC 2021
X64
debian.11-x64
https://dotnetfiddle.net/sZ3OKj
有趣的是,这在 powershell 中似乎不起作用,至少在尝试 运行 时对我来说不起作用:[System.Runtime.InteropServices.RuntimeInformation]::RuntimeIdentifier
在 C# 程序中,我需要获取有关程序所在的运行时环境的信息 运行。
本质上,我需要知道当前程序是 运行 in .NET Core 还是 Full .NET Framework 4.x。
类似以下的方法可能有效:
public string GetRuntimeVersion()
{
#if NET451
return "net451";
#elseif netcoreapp11
return "netcoreapp11";
#elseif netstandard14
return "netcoreapp14";
#endif
...
}
但是有没有更好的方法呢?
勾选 System.Environment.Version
属性 (http://msdn.microsoft.com/en-us/library/system.environment.version.aspx).
public static void Main()
{
Console.WriteLine("Version: {0}", Environment.Version.ToString());
}
Microsoft.Extensions.PlatformAbstractions !
using Microsoft.Extensions.PlatformAbstractions;
var runtimeInfo = PlatformServices.Default.Application.RuntimeFramework;
PlatformServices.Default.Application.RuntimeFramework
属性 包含运行时标识符及其版本等信息。并且在 .net 核心上可用。
致谢:
Victor Hurdugaci for
Jurjen for pointing out
System.Environment.Version
which somehow led me to Victor Hurdugaci的评论
现在好像还有System.Runtime.InteropServices.RuntimeInformation
。
PROPERTIES FrameworkDescription
Gets the name of the .NET installation on which an app is running.
OSArchitecture
Gets the platform architecture on which the current app is running.
OSDescription Gets a string that describes the operating system on which the app is running.
ProcessArchitecture
Gets the process architecture of the currently running app.
RuntimeIdentifier Gets the platform on which an app is running.
using System;
public class Program
{
public static void Main()
{
Console.WriteLine(System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription);
Console.WriteLine(System.Runtime.InteropServices.RuntimeInformation.OSArchitecture);
Console.WriteLine(System.Runtime.InteropServices.RuntimeInformation.OSDescription);
Console.WriteLine(System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture);
Console.WriteLine(System.Runtime.InteropServices.RuntimeInformation.RuntimeIdentifier);
}
}
.NET 6.0.0-rtm.21522.10
X64
Linux 5.4.0-1064-azure #67~18.04.1-Ubuntu SMP Wed Nov 10 11:38:21 UTC 2021
X64
debian.11-x64
https://dotnetfiddle.net/sZ3OKj
有趣的是,这在 powershell 中似乎不起作用,至少在尝试 运行 时对我来说不起作用:[System.Runtime.InteropServices.RuntimeInformation]::RuntimeIdentifier