4

How to change Dotnet target framework in .csproj using CLI?

I know how to do this using Visual Studio, but I want to do it using CLI.

Are there any commands like,
dotnet changeframework netcoreapp3.1?

Thanks.

redevilliers
  • 75
  • 1
  • 6

2 Answers2

3

Unfortunately, there isn't a built in command.

You can edit the .csproj file as a text file and modify the TargetFramework element directly.

You can override it at build time using the CLI by overriding the msbuild property: dotnet build -p:TargetFramwork=netcoreapp3.1.

You can use command line tools like sed (on Linux/macOS) to modify the file directly: sed -i -E 's|<TargetFramework>.*</TargetFramework>|<TargetFramework>netcoreapp3.1</TargetFramework>|' file.csproj. For Windows, try get-content.

omajid
  • 14,165
  • 4
  • 47
  • 64
3

I have net6.0 and net5.0 installed on my machine, and the default is set to net6.0. I wanted to target net5.0 framework for a webapi project and achieved it by typing this: dotnet new webapi -f net5.0 -n myApiProject

You can also see all the options on any template by typing dotnet new <template (example: webapi, classlib, etc.)> -h. This is where I found the -f|--framework option.

Curious
  • 31
  • 3
  • For reference, this command is for making a _new_ project. There is still no way to modifying an _existing_ project using the CLI. – gunr2171 Dec 05 '22 at 02:16