Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Last active March 15, 2024 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karenpayneoregon/81b514e3ad6fa2c7e335962f79a24ebd to your computer and use it in GitHub Desktop.
Save karenpayneoregon/81b514e3ad6fa2c7e335962f79a24ebd to your computer and use it in GitHub Desktop.
Get .NET Core runtime version

Here is the difference assembly.CodeBase is obsolete according to Micrsoft, the proper usage for nw is assembly.Location

public static string FrameworkRuntimeVersion()
{
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var path = assembly.Location.Split(new[] { '/', '\\' },
StringSplitOptions.RemoveEmptyEntries);
var index = Array.IndexOf(path, "Microsoft.NETCore.App");
return index > 0 && index < path.Length - 2 ? path[index + 1] : null;
}
public static string GetNetCoreVersion()
{
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment