How can I get the current CLR Runtime version in a running .NET program ?
Try Environment.Version to get that info. Also you may need to call ToString().
Check out the System.Environment.Version property.
https://docs.microsoft.com/en-us/dotnet/api/system.environment.version
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"
Since .NET 4.5 you can't really use System.Environment.Version (it will 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).
Instead (as @jim-w mentioned) you have to check the registry against a "simple" lookup table. It's a bit ridiculous and being Windows-specific, does not work for .NET Core ...
However ... starting in .NET 4.7.1, they've back-ported a class from .NET Core into the full framework, and you can now check System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription
Unfortunately, it returns a string with either: ".NET Core", ".NET Framework", or ".NET Native" before the version number -- so you still have some parsing to do.