Home > database >  Updating net5.0 .csproj to new Runtime
Updating net5.0 .csproj to new Runtime

Time:11-22

my current .csproj has the following project stats:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <UserSecretsId>aspnet-XYZ</UserSecretsId>
  </PropertyGroup>

I also have several entries in my ItemGroup indicating "Version="5.0.10".

Since I am upgrading, I uninstalled old runtimes/frameworks and

dotnet --list-runtimes

shows:

Microsoft.AspNetCore.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Manually changing the project entries was not successful.

Is there a way to point this project to the new runtime installation?

CodePudding user response:

Visual Studio

If you are using Visual Studio, all you need to do is:
Right-click on your project in solution explorer, Properties > Application > Target Framework, then choose .NET 6.0. (Currently, this works only in VS 2022)

To update all packages you have installed:
Right-click on your project in solution explorer, Manage NuGet packages... > Update Tab > Check Select all packages > click on Update button.

Manually

All you need to do is change the .csproj file like:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    ...
  </PropertyGroup>
  ...
</Project>

More info Migrate from ASP.NET Core 5.0 to 6.0

To update all packages you have installed, use this PowerShell script:

$regex = 'PackageReference Include="([^"]*)" Version="([^"]*)"'

ForEach ($file in get-childitem . -recurse | where {$_.extension -like "*proj"})
{
    $packages = Get-Content $file.FullName |
        select-string -pattern $regex -AllMatches | 
        ForEach-Object {$_.Matches} | 
        ForEach-Object {$_.Groups[1].Value.ToString()}| 
        sort -Unique
    
    ForEach ($package in $packages)
    {
        write-host "Update $file package :$package"  -foreground 'magenta'
        $fullName = $file.FullName
        iex "dotnet add $fullName package $package"
    }
}

Base on Rolf Wessels's answer.

But I recommend using the dotnet-outdated tool.

  • Related