Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can MSBuild detect the version of .NET Core?

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?

like image 932
minhee Avatar asked Dec 27 '19 05:12

minhee


People also ask

How do I check my .NET Core version?

NET Core is installed on Windows is: Press Windows + R. Type cmd. On the command prompt, type dotnet --version.

Does MSBuild work with .NET Core?

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.

What is MSBuild in the .NET Core?

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.

Does .NET framework include MSBuild?

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.

How do I check which version of NET Core is installed?

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.

What is the MSBuild core version of MSBuild?

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.

How to check which version of dotNET is running on Windows?

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.

What is NET Core?

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.


1 Answers

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>

Update:

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

In MSBuild:

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.

like image 119
Keyur Ramoliya Avatar answered Oct 19 '22 05:10

Keyur Ramoliya