在 dotnet 核心 SDK 版本之间切换

我最近安装 VS2017RC,然后自动我的网点版本指向 1.0.0-preview4-004233。由于这一点,每当我创建一个新的项目使用命令 dotnet new -t Console我不能看到 project.json,虽然我看到 .csproj文件。

当我检查我的机器在 -C:\Program Files\dotnet\sdk上可用的 dotnet 版本时,我看到有多个版本可用。

有没有办法把 dotnet 核心从 1.0.0-preview4-004233切换回早期版本的 1.0.0-preview2-003133,而不用卸载。

76950 次浏览

You can do this with a global.json file in the root of your project:

  • Verify the list of SDKs on your machine:
dotnet --list-sdks

You'll see a list like this.

2.1.100 [C:\Program Files\dotnet\sdk]
2.1.101 [C:\Program Files\dotnet\sdk]
2.1.103 [C:\Program Files\dotnet\sdk]
2.1.104 [C:\Program Files\dotnet\sdk]
[...lines omitted...]
2.1.601 [C:\Program Files\dotnet\sdk]
2.2.101 [C:\Program Files\dotnet\sdk]
3.0.100-preview3-010431 [C:\Program Files\dotnet\sdk]
  • Create a folder to be the root of your project, where you are going to run dotnet new.
  • In that folder, run this command: dotnet new globaljson

The result will look something like this:

{
"sdk": {
"version": "3.0.100-preview3-010431"
}
}
  • version中,将3.0.100-preview3-010431替换为--list-sdks列表中您喜欢的版本。例如:
{
"sdk": {
"version": "2.2.101"
}
}
  • Run dotnet --version to verify. You should see:
2.2.101
  • Run the appropriate dotnet new commands to create your project.

Dotnet usually uses the latest SDK version, unless it finds a global.json file that tells it to do otherwise. The explanation by microsoft

dotnet looks for the file in the working directory (not necessarily the project or solution directory), and if it can't find one it starts searching upwards from there. documentation

An easy way to create a global.json file would be to run dotnet new globaljson --sdk-version 1.0.0-preview2-003133 in the directory of your project. create a global.json from the cli

You can check in global.json

Run the command dotnet --list-sdks

You will see the results

2.2.110 [C:\Program Files\dotnet\sdk] 5.0.103 [C:\Program Files\dotnet\sdk]

When we install each dotnet core SDK on OS, the each project can use SDKs version separately. Because the SDK have global installation. We can configuration each project settings by create global.json via this command:

dotnet new globaljson

and finally selected the correct version.

The process for selecting an SDK version is:

  • dotnet searches for a global.json file iteratively reverse-navigating the path upward from the current working directory.
  • dotnet uses the SDK specified in the first global.json found.
  • dotnet uses the latest installed SDK if no global.json is found.

References: https://learn.microsoft.com/en-us/dotnet/core/tools/global-json?tabs=netcore3x#globaljson-and-the-net-core-cli

Step-by-Step: https://stackoverflow.com/a/42078060/14557383

If you want to create a new project using a specific version you can go to this directory 'C:\Program Files\dotnet\sdk' then add an underscore to newer version (all of them) of dotnet that you don't want (only the newer ones is enough) then create your project.

Image of the directory which has the dotnet versions.