如何在nuget.config repositoryPath和csproj中使用变量参考HintPath [英] How to use variables in nuget.config repositoryPath and csproj Reference HintPath

查看:300
本文介绍了如何在nuget.config repositoryPath和csproj中使用变量参考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).

我试图创建一个nuget.config,如下所示:

I've tried to create a nuget.config like the following:

<?xml version="1.0" encoding="utf-8"?>
<configuration>  
  <config>
    <add key="repositoryPath" value="%USERPROFILE%\.nuget\shared_packages" />
  </config>
</configuration>

当我安装软件包时,它会生成带有相对路径的提示路径:

And when I install a package it generates a Hint Path with a relative path:

<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文件编辑为以下内容:

This won't work on other machines so I have to manually edit the .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文件?

Can I configure 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 repositoryPath和csproj参考HintPath中使用变量

How to use variables in nuget.config repositoryPath and csproj Reference HintPath

就像Matt所说的那样," NuGet始终使用相对路径作为引用,如果您正在使用packages.config文件.".而且,AFAIK中,没有这样的configurein nuget.config(或其他东西?)来使提示路径使用$(USERPROFILE)\.nuget\shared_packages的文字值.

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 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项目所做的操作).

To resolve this question, you can use Package references (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"->包管理设置":

To use the PackageReference, go to the Tools->NuGet PackageManager->Package Manage Settings:

然后,您可以将软件包添加到项目中,所有软件包都存储在C:\Users\<UserName>\.nuget\packages中.

Then you can add your packages to your project, all the packages are stored in the C:\Users\<UserName>\.nuget\packages.

此外,您可以迁移对于那些使用packages.config安装软件包的项目,从packages.config到PackageReference .

Besides, you can Migrate from packages.config to PackageReference for those projects which install the packages with packages.config.

希望这会有所帮助.

这篇关于如何在nuget.config repositoryPath和csproj中使用变量参考HintPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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