如何在nuget.config repositoryPath和csproj中使用变量参考HintPath [英] How to use variables in nuget.config repositoryPath and csproj Reference HintPath
问题描述
由于我们使用
在多个解决方案中包含一个项目是很常见的.不幸的是,NuGet无法理解这种情况.而且由于NuGet在解决方案级别使用packages文件夹,因此项目中的提示路径"变得与解决方案相关.如果一个项目有多个解决方案,则这些相对路径很容易中断.
It's pretty common to include a project in mutliple solutions. Unfortunately, NuGet cannot understand when this is the case. And because NuGet uses a packages folder at the solution level, the Hint Paths in projects become relative to the solution. If there are multiple solutions using a project, these relative paths can easily break. 我正在尝试找出一种合理的方法来防止提示路径被破坏. I'm trying to figure out a reasonable way to keep the Hint Paths from breaking. 我的一个想法是使用环境变量指向共享的程序包文件夹(类似于NuGet在.NET Core项目中的工作方式). One idea I had was to use an environment variable to point to a shared package folder (similar to how NuGet does it with .NET Core projects). 我试图创建一个 I've tried to create a 当我安装软件包时,它会生成带有相对路径的提示路径: And when I install a package it generates a Hint Path with a relative path: 这在其他计算机上将不起作用,因此我必须手动将 This won't work on other machines so I have to manually edit the 是否可以配置 Can I configure 如何在nuget.config repositoryPath和csproj参考HintPath中使用变量 How to use variables in nuget.config repositoryPath and csproj Reference HintPath 就像Matt所说的那样," NuGet始终使用相对路径作为引用,如果您正在使用packages.config文件.".而且,AFAIK中,没有这样的configurein Just like Matt said, "NuGet always uses relative paths for references if you are using a packages.config file.". And, AFAIK, there is no such configurein 要解决此问题,可以在项目文件中使用程序包引用( To resolve this question, you can use Package references ( 要使用 To use the 然后,您可以将软件包添加到项目中,所有软件包都存储在 Then you can add your packages to your project, all the packages are stored in the 此外,您可以迁移对于那些使用 Besides, you can Migrate from packages.config to PackageReference for those projects which install the packages with 希望这会有所帮助. 这篇关于如何在nuget.config repositoryPath和csproj中使用变量参考HintPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
nuget.config
,如下所示:nuget.config
like the following:<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value="%USERPROFILE%\.nuget\shared_packages" />
</config>
</configuration>
<Reference Include="QRCoder, Version=1.3.5.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\..\..\Users\Username\.nuget\shared_packages\QRCoder.1.3.5\lib\net40\QRCoder.dll</HintPath>
</Reference>
.csproj
文件编辑为以下内容:.csproj
file to something like this:<Reference Include="QRCoder, Version=1.3.5.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>$(USERPROFILE)\.nuget\shared_packages\QRCoder.1.3.5\lib\net40\QRCoder.dll</HintPath>
</Reference>
nuget.config
(或其他功能?)使提示路径使用$(USERPROFILE)\.nuget\shared_packages
的文字值,这样我不必每次都手动编辑.csproj
文件?nuget.config
(or something else?) to make the Hint Path use the literal value of $(USERPROFILE)\.nuget\shared_packages
so I don't have to manually edit the .csproj
file every time?推荐答案
nuget.config
(或其他东西?)来使提示路径使用$(USERPROFILE)\.nuget\shared_packages
的文字值. nuget.config
(or something else?) to make the Hint Path use the literal value of $(USERPROFILE)\.nuget\shared_packages
. PackageReference
)而不是packages.config.使用PackageReference
,即使您使用的是.NET框架,您的程序包也会指向共享的程序包文件夹(类似于NuGet对.NET Core
项目所做的操作).PackageReference
) instead of packages.config in project files. With PackageReference
, your packages are point to the shared package folder (similar to how NuGet does it with .NET Core
projects), even if you are using the .NET framework.PackageReference
,请转到工具"->"NuGet PackageManager"->包管理设置":PackageReference
, go to the Tools->NuGet PackageManager->Package Manage Settings:C:\Users\<UserName>\.nuget\packages
中.C:\Users\<UserName>\.nuget\packages
.packages.config
安装软件包的项目,从packages.config到PackageReference .packages.config
.