1

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.

2 Answers2

0

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
  • 12,235
  • 4
  • 44
  • 60
0

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.