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.

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

1 Comment

For reference, this command is for making a new project. There is still no way to modifying an existing project using the CLI.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.