Search code examples
c#.net-core.net-standard.net-standard-2.0

How to determine the .NET platform that runs the .NET Standard class library?


.NET Standard Library — One library to rule them all.

The .NET Standard Library functionality may vary depending on the .NET platform that runs it:

  • .NET Framework
  • .NET Core
  • Xamarin

How to check the .NET platform where the .NET Standard library currently runs?

For example:

// System.AppContext.TargetFrameworkName 
// returns ".NETFramework,Version=v4.6.1" for .NET Framework 
// and
// returns null for .NET Core.

if (IsNullOrWhiteSpace(System.AppContext.TargetFrameworkName))
    // the platform is .NET Core or at least not .NET Framework
else
    // the platform is .NET Framework

Is it reliable way to answer the question (at least for .NET Framework and .NET Core)?


Solution

  • Use the RuntimeInformation.FrameworkDescription Property from the System.Runtime.InteropServices namespace.

    Returns a string that indicates the name of the .NET installation on which an app is running.

    The property returns one of the following strings:

    • ".NET Core".

    • ".NET Framework".

    • ".NET Native".