VS2017如何升级csproj文件 [英] How to upgrade csproj files with VS2017

查看:16
本文介绍了VS2017如何升级csproj文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,VS2017 已经正确地将几个基于 project.json/.xproj 的项目转换为新的 .csproj 格式.

VS2017 has so far correctly converted several project.json/.xproj based projects to the new .csproj format.

我还想将新的 .csproj 格式用于以前仅针对 .NET Framework 的旧 .csproj 项目(即它们不适用于 dnx/dotnet CLI).

I would also like to use the new .csproj format with older .csproj projects that previously targeted only .NET Framework (i.e. they didn't work with dnx/dotnet CLI).

似乎即使一个项目仍然只针对 .NET Framework,<PackageReference> 和易于编辑的 .csproj 文件的好处似乎值得(希望不会太大)麻烦.

It seems that even if a project would still only target .NET Framework, the benefits of <PackageReference> and an easily editable .csproj file seem worth the (hopefully not too great) trouble.

这可以直接使用 Visual Studio 2017 吗?

Is this possible to do with Visual Studio 2017 directly?

如果没有,需要哪些手动步骤?

If not, what manual steps would be required?

推荐答案

我正在编辑我的答案,以明确您不需要升级 .csproj 文件.正如德鲁在下面评论的那样,这样做有好处.但是,VS2017 将继续使用经典的 csproj 文件就好了.此外,VS2017 中没有任何内容可以为您执行升级.如果您确实希望利用新格式,那么下面的演练应该会有所帮助.

I'm editing my answer to make it clear that you don't need to upgrade your .csproj file. As Drew commented below there are benefits to doing so. However, VS2017 will continue to work with the classic csproj file just fine. In addition there is nothing in VS2017 that will perform the upgrade for you. If you do wish to take advantage of the new format the walk through below should help.

.csproj 文件升级到新的 Visual Studio 2017 格式对于简单的类库或控制台项目来说很容易.

Upgrading the .csproj file to the new Visual Studio 2017 format is easy for simple class libraries or console projects.

如果您不使用版本控制,请在开始之前确保备份您的 csproj 文件,以及 Properties/AssemblyInfo.cspackages.config.新的 csproj 文件很棒.在许多项目中,我已经用十几行代码替换了数百行代码.但是,由于 Visual Studio 2017 继续支持以前的 csproj 文件,这可能是过早优化的情况.如果您的解决方案包含数十个项目、许多 NuGet 包以及对 csproj 的任何自定义,那么您可能会执行一个不必要的 make work 项目.

If you're not using version control, before you begin be sure to backup your csproj file, and both Properties/AssemblyInfo.cs, and packages.config. The new csproj file is great. In many projects I've replaced hundreds of lines of code with a dozen or so. However, as Visual Studio 2017 continues to support the previous csproj files this may be a case of premature optimization. If you have a solution that contains dozens of projects, many NuGet packages, and any customization to csproj you are likely undertaking an unnecessary make work project.

用适当的代码替换 .csproj 文件的全部内容,如下所示.

Replace the entire contents of your .csproj file with the appropriate code as follows.

类库

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net462</TargetFramework>
  </PropertyGroup>
</Project>

控制台应用程序

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net462</TargetFramework>
  </PropertyGroup>
</Project>

<TargetFramework>属性更改为您需要的 .NET 版本,例如 net452、net46、net461 等.

Change the <TargetFramework>attribute to the .NET version you require such as net452, net46, net461 etc.

默认情况下,编译器会拾取项目文件夹中的所有代码.如果您在项目文件夹之外有代码,则必须像在以前版本的 Visual Studio 和 csproj 中一样显式引用它.

By default all code within your project folder will be picked up by the compiler. If you have code outside your project folder you must explicitly reference it the same way you do in previous versions of Visual Studio and csproj.

进行上述更改后,在 Visual Studio 2017 中加载您的解决方案.此时应该构建最基本的项目.如果不是,您可能需要添加缺少的程序集或项目引用.添加引用与在以前版本的 Visual Studio 中添加引用非常相似.在解决方案资源管理器中选择您的项目,右键单击 Dependencies,然后选择 Add Reference.添加您缺少的任何框架或项目引用.

After making the above changes load your solution in Visual Studio 2017. At this point the most basic projects should build. If not you likely need to add missing assembly or project references. Adding references is very similar to doing so in previous versions of Visual Studio. Select your project in the Solution Explorer, right click on Dependencies, and select Add Reference. Add any Framework or Project references that you're missing.

尝试再次构建您的解决方案/项目.您可能会收到有关重复属性的错误.此错误是因为之前在 AssemblyInfo.cs 中定义的属性已被移动到 csproj 文件中.删除 Properties 文件夹下的 AssemblyInfo.cs 文件应该可以解决这些错误.在删除 AssemblyInfo.cs 之前,您应该移动您定义的所有数据.大多数属性都可以在项目文件的包信息部分输入.右键单击您的项目名称,选择 Package 页面,然后输入之前在 AssemblyInfo.cs 文件中定义的任何数据.这包括汇编版本、作者、版权等项目.

Attempt to build your solution/project again. You may receive errors about duplicate attributes. This error is because attributes previously defined in AssemblyInfo.cs have been moved to the csproj file. Deleting the AssemblyInfo.cs file, found under the Properties folder, should resolve these errors. Before deleting the AssemblyInfo.cs you should move any data you have defined. Most attributes can be entered in the package information section of your project file. Right click on your project name, select the Package page, and enter any data that was previously defined in your AssemblyInfo.cs file. This includes items such as the assembly version, author, copyright etc.

下面是显示上一步的屏幕截图.

Below is a screen shot that shows the previous step.

如果您在项目中使用任何 NuGet 包,您也需要将它们移动到新格式.在 Visual Studio 2017 之前,NuGet 依赖于项目根目录中名为 Packages.config 的文件以及 csproj 中的引用.要迁移您的 NuGet 包引用,请右键单击您的解决方案并加载 Nuget 包管理器.在右上角加载后,单击齿轮,将加载 NuGet 包管理器选项.选择常规.在 Package Management 下,将选项 Default package management format 更改为 PackageReference.此时,您必须手动将所有 NuGet 包添加回您的解决方案.您可以在项目根文件夹的 packages.config 文件中找到所有包.一旦你添加了所有的包,你可以删除 packages.config 文件.

If you are using any NuGet packages in your project you need to move those to the new format as well. Previous to Visual Studio 2017 NuGet relied on a file named Packages.config in the root of your project in addition to references in csproj. To migrate your NuGet package references right click on your solution and load the Nuget Package Manager. Once loaded in the upper-right hand corner, click the cog, and the NuGet Package Manager Options will load. Select General. Under Package Management change the option Default package management format to PackageReference. At this point you'll have to manually add all your NuGet packages back to your solution. You can find all the packages in the packages.config file in the project's root folder. Once you have added all the packages back you can delete the packages.config file.

这篇关于VS2017如何升级csproj文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆