Skip to content

Instantly share code, notes, and snippets.

@richlander
Last active October 20, 2020 01:13
Show Gist options
  • Select an option

  • Save richlander/3f9bd226acaa3c8e080e1945e7407bc3 to your computer and use it in GitHub Desktop.

Select an option

Save richlander/3f9bd226acaa3c8e080e1945e7407bc3 to your computer and use it in GitHub Desktop.
Testing for .NET family
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp3.1;net5.0;net48</TargetFrameworks>
</PropertyGroup>
</Project>
using System;
using System.Runtime.InteropServices;
namespace frameworkdescription
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"{nameof(Environment.Version)}: {Environment.Version}");
Console.WriteLine($"{nameof(RuntimeInformation.FrameworkDescription)}: {RuntimeInformation.FrameworkDescription}");
if (Environment.Version.Major >= 5)
{
Console.WriteLine($"This is .NET {Environment.Version}");
}
// This test for .NET Core only works for 3.0+
// Context: https://github.com/dotnet/runtime/issues/12124
else if (RuntimeInformation.FrameworkDescription.StartsWith(".NET Core"))
{
Console.WriteLine($"This is .NET Core {Environment.Version}");
}
else if (Environment.Version.Major < 5)
{
Console.WriteLine($"This is .NET Framework {Environment.Version}");
}
// Some folks want a simple "is .NET Core" check.
var isDotnetCore = Environment.Version.Major >= 5 || RuntimeInformation.FrameworkDescription.StartsWith(".NET Core");
if (isDotnetCore)
{
Console.WriteLine("This is .NET Core");
}
}
}
}
@richlander
Copy link
Copy Markdown
Author

richlander commented Oct 19, 2020

Results:

PS D:\git\testapps\frameworkdescription> dotnet run -f net5.0
Version: 5.0.0
FrameworkDescription: .NET 5.0.0-rc.2.20475.5
This is .NET 5.0.0
This is .NET Core
PS D:\git\testapps\frameworkdescription> dotnet run -f netcoreapp3.1
Version: 3.1.7
FrameworkDescription: .NET Core 3.1.7
This is .NET Core 3.1.7
This is .NET Core
PS D:\git\testapps\frameworkdescription> dotnet run -f net48
Version: 4.0.30319.42000
FrameworkDescription: .NET Framework 4.8.4261.0
This is .NET Framework 4.0.30319.42000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment