Switch between different .NET Core SDKs at Command Line

As you may already know, you can download and install on your PC many different versions of the .NET Core SDK.

In the moment I write this Post, from the official Microsoft website I can see the following:

Available .NET Core from Microsoft WebSite

And to tell you what I’m currently doing: I’m working on the latest .NET 5.0 Preview to play around a bit 🙂

Once you install a newer .NET Core package, the latest is going to be the default used in your CLI. For me as example:

Please mind, that this means that when you create a new Project with the command line, the new project will be based on this version of .NET Core.

What about if you want to create a new project with a different version of just switch to a different Framework version? Let’s have a look!

Well, first of all, with the command

dotnet --list-sdks

You can get the list of the available SDKs

and with this the list of the available runtimes

dotnet --list-runtimes

new let’s create a new console app with

dotnet new console

Open in your favorite editor the .csproj file and you should see something like this

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>
</Project>

Editing this file, specifying a different version of the framework in the tag “TargetFramework” you can of course achieve your goal. But what about changing the target SDK before to create the project? Well, this result can be easily achieved, before starting with the new project, creating a global.json file in the current working directory with the command:

dotnet new globaljson

running this without further parameter, will create a globaljson pointing to the latest available SDK, in case you want to use, for example, the version 3.1.301, all what you need to do is:

dotnet new globaljson --sdk-version 3.1.301

and your new global.json file will look similar to this:

{
  "sdk": {
    "version": "3.1.301"
  }
}

now, the result of the dotnet –version command, will be like this

This of course won’t change the default .NET Core version everywhere, but in the folder where you created the global.json and all its subfolder 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s