Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dotnet - How to change Target Framework using CLI?

Tags:

c#

.net

.net-core

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.

like image 913
redevilliers Avatar asked Oct 17 '25 13:10

redevilliers


2 Answers

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.

like image 120
omajid Avatar answered Oct 20 '25 03:10

omajid


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.

like image 39
Curious Avatar answered Oct 20 '25 02:10

Curious



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!