简体   繁体   English

如何更改 Visual Studio 项目中的 NuGet 包引用以使用 Nuget.config

[英]How to change the NuGet-package reference in a Visual Studio project to use Nuget.config

I have a Visual Studio project file with the extension .csproj.我有一个扩展名为 .csproj 的 Visual Studio 项目文件。 Inside it are references like this:里面是这样的引用:

<Import Project="..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props"....

I have now made a NuGet.config file in the parent folder of from the solution folder.我现在已经在解决方案文件夹的父文件夹中创建了一个 NuGet.config 文件。 And I removed the local "packages" folder.我删除了本地“包”文件夹。 In the new nuget.config I set up a common location for storing packages.在新的 nuget.config 中,我设置了一个用于存储包的公共位置。

nuget.config: nuget.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <config>
        <add key="repositoryPath" value="D:\Data\NuGet" />
    </config>
    <packageRestore>
        <add key="enabled" value="True" />
    </packageRestore>
</configuration>

When I building I now get this error:当我构建时,我现在收到此错误:

This project references NuGet package(s) that are missing on this computer.此项目引用此计算机上缺少的 NuGet 包。 Use NuGet Package Restore to download them.使用 NuGet Package Restore 来下载它们。 For more information... The missing file is ..\\packages\\Microsoft.Net.Compilers.1.0.0\\build\\Microsoft.Net.Compilers.props.有关详细信息... 丢失的文件是 ..\\packages\\Microsoft.Net.Compilers.1.0.0\\build\\Microsoft.Net.Compilers.props。

How can I solve this?我该如何解决这个问题?

If I manually have to replace the (Import Project="..\\packages...) elements in the project file, what should I change it to, so that it follows the configuration from the Nuget.config?如果我必须手动替换项目文件中的 (Import Project="..\\packages...) 元素,我应该将其更改为什么,以便它遵循 Nuget.config 中的配置?

If I manually have to replace the (Import Project="..\\packages...) elements in the project file, what should I change it to, so that it follows the configuration from the Nuget.config?如果我必须手动替换项目文件中的 (Import Project="..\\packages...) 元素,我应该将其更改为什么,以便它遵循 Nuget.config 中的配置?

Since you use the new nuget.config file which changed the path of the local nuget reference(like this <add key="repositoryPath" value="xxxxxx" /> ).由于您使用了新的nuget.config文件,该文件更改了本地 nuget 引用的路径(例如<add key="repositoryPath" value="xxxxxx" /> )。

And Restore will only restore the missing nuget packages but will not change to use the new nuget package location in xxx.csproj .恢复将仅恢复丢失的NuGet软件包,但不会改变在使用新的NuGet包的位置xxx.csproj

So you can follow my steps to resolve the issue:所以你可以按照我的步骤来解决这个问题:

Solution解决方案

1) Tools-->Nuget Package Manager-->Package Manager Console--> 1)工具-->Nuget 包管理器-->包管理器控制台-->

type Update-Package -reinstall to reinstall these packages to reference the new right path.键入Update-Package -reinstall以重新安装这些包以引用新的正确路径。

2) enter the xxxx.csproj file, delete these duplicate, old import info like these: 2)进入xxxx.csproj文件,删除这些重复的旧import信息,如下所示:

 <Import Project="..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props" Condition="Exists('..\..\..\..\..\..\installed_packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props')" />

 <Error Condition="!Exists('..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props'))" />

3) Then rebuild your project and will solve this issue. 3)然后重建你的项目,将解决这个问题。

Update 1更新 1

The new Nuget.config file will specify that the newly installed nuget packages use the new reference address, but for previously installed nuget packages, the reference address in the xxx.csporj file will remain the old address.新的Nuget.config文件将指定新安装的 nuget 包使用新的引用地址,但对于以前安装的 nuget 包, xxx.csporj文件中的引用地址将保持旧地址。 The Restore procedure only restores the nuget package under the new path, but it does not make any changes to the nuget reference in xxx.csproj file, so it can only be reinstalled.还原过程只还原新路径下的nuget包,但不会对xxx.csproj文件中的nuget引用做任何改动,所以只能重新安装。

Besides , the import node is created by Microsoft.Net.Compilers props file from the build folder in the microsoft.net.compilers nuget package.此外,导入节点由microsoft.net.compilers nuget 包中的build文件夹中的Microsoft.Net.Compilers props 文件创建。 And it is a nuget mechanism which can do some operation in xxx.csproj file when you install the nuget package. 它是一种nuget机制,可以在安装nuget包时在xxx.csproj文件中进行一些操作。

However , this file is also special and when you change the nuget reference path.但是,此文件也很特殊,当您更改 nuget 引用路径时。 Because nuget enabled the new address mechanism, during the uninstallation process, the old address of Microsoft.Net.Compilers.props is still not recognized, so it cannot be uninstalled.由于nuget启用了新地址机制,在卸载过程中,仍然无法识别Microsoft.Net.Compilers.props的旧地址,因此无法卸载。 In fact , when you execute the reinstall nuget package, a new address has been created in the xxx.csproj file.其实在执行reinstall nuget包的时候,已经在xxx.csproj文件中新建了一个地址。 See this:看到这个:

在此处输入图片说明

So you should just delete duplicate files from the old address.所以你应该从旧地址中删除重复的文件。

Visual Studio option to change the Nuget Package References用于更改 Nuget 包引用的 Visual Studio 选项

  • In Visual Studio Tools=> Nuget Package Manager => Package Sources.在 Visual Studio 工具 => Nuget 包管理器 => 包源中。
  • You can change the package sources here.您可以在此处更改包源。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改 nuget 包文件夹并在 Visual Studio 中使用另一个 NuGet.Config - Change nuget packages folder and use another NuGet.Config in Visual Studio NuGet.Config 文件位于 Visual Studio 项目中的什么位置? - Where is NuGet.Config file located in Visual Studio project? 如何在 nuget.config repositoryPath 和 csproj Reference HintPath 中使用变量 - How to use variables in nuget.config repositoryPath and csproj Reference HintPath Nuget软件包未添加软件包参考 - Nuget-package not adding package reference 2015 visual studio Nuget程序包管理器不允许我保存新的程序包源,并且Nuget.Config无效的XML - 2015 visual studio Nuget package manager does not let me save new package sources and Nuget.Config is not valid XML 如何在Visual Studio中提供Nuget包的引用 - How to give reference of Nuget package in Visual Studio 创建已包含 Nuget Package 参考的 Visual Studio 项目模板? - Creating a Visual Studio Project Template that already includes a Nuget Package Reference? 在Visual Studio中向项目添加引用和添加Nuget包之间的区别 - Difference between adding a Reference and adding a Nuget package to a project in Visual Studio 如何在nuget.config中设置相对路径? - how to set relative path in nuget.config? 引用 a.Net Core 3.1 NuGet package 的 a.Net 5 Visual Studio 项目/解决方案如何编译成功? - How can a .Net 5 Visual Studio Project / Solution that reference a .Net Core 3.1 NuGet package compile successfuly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM