[英]Change nuget packages folder and use another NuGet.Config in Visual Studio
[英]How to change the NuGet-package reference in a Visual Studio project to use Nuget.config
我有一个扩展名为 .csproj 的 Visual Studio 项目文件。 里面是这样的引用:
<Import Project="..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props"....
我现在已经在解决方案文件夹的父文件夹中创建了一个 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>
当我构建时,我现在收到此错误:
此项目引用此计算机上缺少的 NuGet 包。 使用 NuGet Package Restore 来下载它们。 有关详细信息... 丢失的文件是 ..\\packages\\Microsoft.Net.Compilers.1.0.0\\build\\Microsoft.Net.Compilers.props。
我该如何解决这个问题?
如果我必须手动替换项目文件中的 (Import Project="..\\packages...) 元素,我应该将其更改为什么,以便它遵循 Nuget.config 中的配置?
如果我必须手动替换项目文件中的 (Import Project="..\\packages...) 元素,我应该将其更改为什么,以便它遵循 Nuget.config 中的配置?
由于您使用了新的nuget.config
文件,该文件更改了本地 nuget 引用的路径(例如<add key="repositoryPath" value="xxxxxx" />
)。
和恢复将仅恢复丢失的NuGet软件包,但不会改变在使用新的NuGet包的位置xxx.csproj
。
所以你可以按照我的步骤来解决这个问题:
解决方案
1)工具-->Nuget 包管理器-->包管理器控制台-->
键入Update-Package -reinstall
以重新安装这些包以引用新的正确路径。
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)然后重建你的项目,将解决这个问题。
更新 1
新的Nuget.config
文件将指定新安装的 nuget 包使用新的引用地址,但对于以前安装的 nuget 包, xxx.csporj
文件中的引用地址将保持旧地址。 还原过程只还原新路径下的nuget包,但不会对xxx.csproj
文件中的nuget引用做任何改动,所以只能重新安装。
此外,导入节点由microsoft.net.compilers
nuget 包中的build
文件夹中的Microsoft.Net.Compilers
props 文件创建。 它是一种nuget机制,可以在安装nuget包时在xxx.csproj
文件中进行一些操作。
但是,此文件也很特殊,当您更改 nuget 引用路径时。 由于nuget启用了新地址机制,在卸载过程中,仍然无法识别Microsoft.Net.Compilers.props
的旧地址,因此无法卸载。 其实在执行reinstall nuget包的时候,已经在xxx.csproj
文件中新建了一个地址。 看到这个:
所以你应该从旧地址中删除重复的文件。
用于更改 Nuget 包引用的 Visual Studio 选项
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.