C# 利用.NET 升级助手将.NET Framework项目升级为.NET 6

本文档详细介绍了如何将一个.NET Framework项目迁移到.NET6,包括环境准备、使用.NET Portability Analyzer分析依赖、安装升级助手、项目升级步骤、解决升级后的兼容问题,以及最终运行项目的整个过程。通过此指南,开发者可以顺利将他们的.NET Framework应用移植到.NET6平台。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

概述

环境准备

.NET Portability Analyzer安装和使用

升级助手介绍和安装

项目升级

打开项目

运行项目

项目下载


 

概述

.NET6 正式版本已经发布有一阵子了,今天我就体验一下如何将.NET Framework的项目升级为.NET 6.

升级条件:

  • Windows 操作系统

  • .NET 6 SDK

  • Visual Studio 2022 17.0 或更高版本

环境准备

①首先是VS2022下载,直接上微软官方网站,下载地址:

https://visualstudio.microsoft.com/zh-hans/downloads/

下载后,在线安装就行,如果之前有vs的其它版本也不用卸载,VS可以多个版本共存,安装的时候.NET 6的运行时默认会勾选安装,如下图:

②如果需要单独下载SDK和运行时,可以上微软官网,地址:

.NET SDKs downloads for Visual Studio (microsoft.com)

③安装好以后,可以在window命令窗口确认.NET 6是否安装正常,可以输入:


vs安装完成后,在新建项目时应该已经可以看到.NET 6的框架了

.NET Portability Analyzer安装和使用

      升级前最好先分析下你的项目依赖的类库是否支持最新的.NET平台,这里可以利用扩展工具.NET Portability Analyzer去完成(仅支持.NET 5以下版本):

扩展工具下载安装完成后,重启VS。在工程或者解决方案的右键菜单中,选择:Portability Analyzer Settings。在配置页面,选择要分析的.net  版本即可。

设置完成后,在VS的工程或者解决方案右键菜单中,选择:Analyze Assembly Portability,然后过一会儿就会出现结果。第二个Sheet页面详细的说明了不支持的API信息。

升级助手介绍和安装

   我们之前的应用在 .NET Framework 上运行,现在想将它们移植到 .NET 6。 当然你可以选择手动移植,但是更方便的方法是利用微软提供的升级助手去完成这个操作.

首先是升级助手安装,方法很简单,在命令窗口执行:

dotnet tool install -g upgrade-assistant

安装成功后,应该如下图所示:

同样地,由于 .NET 升级助手是作为 .NET 工具安装的,因此运行以下命令可以轻松地更新它:

dotnet tool update -g upgrade-assistant

项目升级

 ①分析 : .NET 升级助手工具包括一种分析模式,它可以提供有关在升级开始之前可能需要进行的更改的见解。在命令窗口 运行 upgrade-assistant analyze 命令,传入正在升级的项目或解决方案的名称。如下图:

输出中有很多内部诊断信息,但某些信息非常有用。请注意,分析模式指示升级将建议项目以 net6.0-windows 目标框架名字对象 (net6.0-windows) 为目标。这是因为解决方案引用的项目是 WPF 项目,是一种仅限 Windows 的技术。控制台应用程序可能会获得直接升级到 TFM net6.0 的建议,除非它使用某些特定于 Windows 的库。

如果报告了任何错误或警告,请在开始升级前处理这些错误或警告。

②升级: 运行 upgrade-assistant upgrade 命令,传入正在升级的项目或解决方案的名称。

该工具将运行并显示它将执行的步骤列表。完成每个步骤后,该工具将提供一组命令,让用户应用或跳过下一步或其他选项。按 Enter 而不选择数字会选择列表中的第一项。杰西莱一顿按键enter,完成后如下图:

详细步骤可参考官网文档--链接地址:

https://docs.microsoft.com/zh-cn/dotnet/core/porting/upgrade-assistant-wpf-framework

打开项目

升级完成后,选择VS2022打开项目,我旧项目依赖的Caliburn.Micro版本是3.2的不支持.NET 6,它已经自动升级到最高版本,解决方法项目数如如下图:

变更项:

①packages.config被迁移到项目.csproj文件;如下图

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0-windows</TargetFramework>
    <OutputType>WinExe</OutputType>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <UseWPF>true</UseWPF>
    <ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
    <PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
    <PackageReference Include="Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers" Version="0.3.310801">
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Caliburn.Micro" Version="4.0.173" />
  </ItemGroup>
</Project>

②Caliburn.Micro:升级到最新支持.NET6的4.0版本;

③AssemblyInfo.cs:程序集版本按理在.NET6 中应该是在项目-》属性中设置,如下图:

但是升级助手升级后没有清理多余的项,那我就手动删除下,最后如下图:

运行项目

项目下载

链接:https://pan.baidu.com/s/1pCdAdAJ-XVG8onsZ9OCYdQ

提取码:0000

This new 7th edition of Pro C# 6.0 and the .NET 4.6 Platform has been completely revised and rewritten to reflect the latest changes to the C# language specification and new advances in the .NET Framework. You'll find new chapters covering all the important new features that make .NET 4.6 the most comprehensive release yet, including:, A Refined ADO.NET Entity Framework Programming ModelNumerous IDE and MVVM Enhancements for WPF Desktop DevelopmentNumerous updates to the ASP.NET Web APIs, This comes on top of award winning coverage of core C# features, both old and new, that have made the previous editions of this book so popular. Readers will gain a solid foundation of object-oriented development techniques, attributes and reflection, generics and collections as well as numerous advanced topics not found in other texts (such as CIL opcodes and emitting dynamic assemblies)., The mission of this book is to provide you with a comprehensive foundation in the C# programming language and the core aspects of the .NET platform plus overviews of technologies built on top of C# and .NET (ADO.NET and Entity Framework, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), ASP.NET (WebForms, MVC, WebAPI).). Once you digest the information presented in these chapters, you’ll be in a perfect position to apply this knowledge to your specific programming assignments, and you’ll be well equipped to explore the .NET universe on your own terms., What You Will Learn:, Be the first to understand the .NET 4.6 platform and C# 6.Discover the ins and outs of the leading .NET technology.Learn from an award-winning author who has been teaching the .NET world since version 1.0.Find complete coverage of XAML, .NET 4.6 and Visual Studio 2015 together with discussion of the new Windows Runtime.
Paperback: 1625 pages Publisher: Apress; 7th ed. 2015 edition (January 1, 2016) Language: English ISBN-10: 1484213335 ISBN-13: 978-1484213339 This new 7th edition of Pro C# 6.0 and the .NET 4.6 Platform has been completely revised and rewritten to reflect the latest changes to the C# language specification and new advances in the .NET Framework. You'll find new chapters covering all the important new features that make .NET 4.6 the most comprehensive release yet, including: A Refined ADO.NET Entity Framework Programming Model Numerous IDE and MVVM Enhancements for WPF Desktop Development Numerous updates to the ASP.NET Web APIs This comes on top of award winning coverage of core C# features, both old and new, that have made the previous editions of this book so popular. Readers will gain a solid foundation of object-oriented development techniques, attributes and reflection, generics and collections as well as numerous advanced topics not found in other texts (such as CIL opcodes and emitting dynamic assemblies). The mission of this book is to provide you with a comprehensive foundation in the C# programming language and the core aspects of the .NET platform plus overviews of technologies built on top of C# and .NET (ADO.NET and Entity Framework, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), ASP.NET (WebForms, MVC, WebAPI).). Once you digest the information presented in these chapters, you’ll be in a perfect position to apply this knowledge to your specific programming assignments, and you’ll be well equipped to explore the .NET universe on your own terms. What you’ll learn Be the first to understand the .NET 4.6 platform and C# 6. Discover the ins and outs of the leading .NET technology. Learn from an award-winning author who has been teaching the .NET world since version 1.0. Find complete coverage of XAML, .NET 4.6 and Visual Studio 2015 together with discussion of the new Windows Runtime. Who this book is for This book is perfect for anyone who is interested in the new .NET Framework 4.6 and the C# language. Whether you are moving to .NET for the first time or are already writing applications using previous .NET versions, this book will provide you with a comprehensive grounding in the new technology and serve as a complete reference throughout your coding career.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值