In MSBuild project file (e.g., *.csproj), I can detect if it is using .NET Core or .NET Framework or Mono by checking MSBuildRuntimeType
property: '$(MSBuildRuntimeType)'!='Core'
. In the similar fashion, can I detect what version of .NET Core (e.g., 2.1
, 2.2
, 3.0
, 3.1
) is it?
NET Core is installed on Windows is: Press Windows + R. Type cmd. On the command prompt, type dotnet --version.
NET Core applications are typically built using MSBuild or dotnet.exe . MSBuild and dotnet.exe don't require Visual Studio to be installed, but in order to use them, the following prerequisites are required on a build server: Visual Studio Build Tools installed.
MSBuild is the build platform for Microsoft and Visual Studio. In UWP application if you open the project folder, then you will see both project. json and *. csproj files.
A target profile is a subset of a target framework. For example, the . NET Framework 4 Client profile does not include references to the MSBuild assemblies.
Check Version. With .NET Core we have 3 ways to check which version Is running on the system, the most basic way will be to run the cmdlet below which will show the running version. To view all .NET Core versions Installed on the system we can navigate to the Installation folder on the path below.
The goal of the .NET Core version of MSBuild is to support building .NET Core projects on Windows, Linux, and Mac OS, without depending on the full .NET Framework or Mono.
Check Version. With .NET Core we have 3 ways to check which version Is running on the system, the most basic way will be to run the cmdlet below which will show the running version. Dotnet --version. To view all .NET Core versions Installed on the system we can navigate to the Installation folder on the path below.
Version 2.0 of.NET CORE Is a cross-platform open source framework that can be run on Windows, Linux or MacOS which was released on 7th March 2017..NET Core Is also a free and open source software framework for developing Console and Web Application for all platforms. The first.NET version 1.0 was released in June 2016.
You can get it like this:
System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.ToString();
This will return the version like this.
.NET Core 3.1.0
You can assign a property in MSBuild like this:
<MSBuildRuntimeVersion>$([System.Runtime.InteropServices.RuntimeInformation]:: FrameworkDescription.ToString())</MSBuildRuntimeVersion>
Based on @minhee's comment I came to know that the above solution is not working .NET Core 2.2 or older version. I did some research on this and found that it was a breaking change in CoreFx in which APIs that report version is now reporting product and not file version.
This thing is introduced in .NET Core 3.0 and I am not aware of that because I am not using .NET Core 2.2 or older.
With that said, I would like to propose another solution which is working with all versions of .NET Core.
Assembly.GetEntryAssembly().GetCustomAttribute<TargetFrameworkAttribute>().FrameworkName;
Here, you will get version number as below.
.NETCoreApp,Version=v3.0
I have tested this code with the following versions in a console application.
.NET Core 2.0
.NET Core 2.1
.NET Core 2.2
.NET Core 3.0
.NET Core 3.1
You can check the same thing in the MSBuild also.
For example, In the below .csproj file I have defined the two message tasks based on the condition of the target framework. So, If I build the project with the target framework as netcoreapp2.2
then it will print "Happy 2020..." in build log and if I build the project with target framework as netcoreapp3.0
then it will print "Happy New Year..." in the build log.
<Project Sdk="Microsoft.NET.Sdk">
<Target Name="FooName" BeforeTargets="Build" Condition="'$(TargetFrameworkVersion)'=='v3.0'">
<Message Text="Happy New Year..." Importance="High" />
</Target>
<Target Name="FooName" BeforeTargets="Build" Condition="'$(TargetFrameworkVersion)'=='v2.2'">
<Message Text="Happy 2020..." Importance="High" />
</Target>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<RootNamespace>New_folder</RootNamespace>
</PropertyGroup>
</Project>
So, you can use TargetFrameworkVersion
property to detect the current version of .NET Core in the MSBuild.
I hope this will help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With