Get Current .NET CLR version at runtime?
How can I ge开发者_如何学Ct the current CLR Runtime version in a running .NET program ?
Check out the System.Environment.Version
property.
https://learn.microsoft.com/en-us/dotnet/api/system.environment.version
Since .NET 4.5 you could just use System.Environment.Version
(it would only return 4.0.{something}, allowing you to verify that you're "at least" on 4.0 but not telling you which actual version is available unless you can map a full list of build numbers in).
In .NET Core, and starting in .NET 4.7.1 of Framework, you can check System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription
It returns a string with either: ".NET Core", ".NET Framework", or ".NET Native" before the version number -- so you have a little parsing to do if you just want the number.
Try Environment.Version
to get that info. Also you may need to call ToString()
.
That works for me:
public static String GetRunningFrameworkVersion()
{
String netVer = Environment.Version;
Assembly assObj = typeof( Object ).GetTypeInfo().Assembly;
if ( assObj != null )
{
AssemblyFileVersionAttribute attr;
attr = (AssemblyFileVersionAttribute)assObj.GetCustomAttribute( typeof(AssemblyFileVersionAttribute) );
if ( attr != null )
{
netVer = attr.Version;
}
}
return netVer;
}
I compiled my .NET program for .NET 4.5 and it returns for running under .NET 4.8:
"4.8.4150.0"
If you just want to find out the version and don't need it to be parsed or of type Version, just do this:
Console.WriteLine(System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription);
If your app is running via .Net Framework 4.7.2, it returns something like .NET Framework 4.7.3875.0
.
If your app is running via .Net 6, it returns something like .NET 6.0.0-rtm.21522.10
.
精彩评论