Search code examples
.netversion

How may I choose which dotnet version, of multiple installed, a command is executed with?


I need to run these 2 commands with dotnet:

dotnet new install Clean.Architecture.Solution.Template
dotnet new ca-sln 

The first is for installing a template.

The second is for creating a project from that template.

I have multiple versions of dotnet installed, and it seems like dotnet 7 (the latest I have installed) is used by default.

How do I make sure the command executes with dotnet 6?


Solution

  • This is managed by global.json

    You can use dotnet new global.json to create a suitable file for the current framework, and then edit it to use any of the frameworks listed in dotnet --info. For example, on my machine dotnet --info says (among other things):

    .NET SDKs installed:
      6.0.411 [C:\Program Files\dotnet\sdk]
      7.0.305 [C:\Program Files\dotnet\sdk]
      7.0.400 [C:\Program Files\dotnet\sdk]
      8.0.100-rc.1.23455.8 [C:\Program Files\dotnet\sdk]
    

    so I could use as my global.json:

    {
      "sdk": {
        "version": "6.0.411"
      }
    }
    

    and now dotnet 6 will be used.

    To see this:

    (before, with no global.json)

    > dotnet --version
    8.0.100-rc.1.23455.8
    

    (after, with a global.json specifying dotnet 6)

    > dotnet --version
    6.0.411
    

    The system searches upwards for global.json, so it can be in the current folder or anywhere higher, and it will be applied.

    Learn more here: https://learn.microsoft.com/en-us/dotnet/core/tools/global-json - including things like roll-forward policies (so you don't need an exact framework match)