对于我的大多数项目,我有以下约定:

/src
    /Solution.sln
    /SolutionFolder
        /Project1
        /Project2
        /etc..
/lib
    /Moq
        moq.dll
        license.txt
    /Yui-Compressor
        yui.compressor.dll
/tools
    /ILMerge
        ilmerge.exe

您会注意到,我没有在源文件夹中保留外部库。我对使用NuGet也很感兴趣,但不希望在源文件夹内使用这些外部库。NuGet是否有一个设置来改变所有包加载到的目录?


当前回答

2.1版本说明中提出的解决方案不能开箱即用。他们忘了提到代码:

internal string ResolveInstallPath()
{
    if (!string.IsNullOrEmpty(this.OutputDirectory))
    {
        return this.OutputDirectory;
    }
    ISettings settings = this._configSettings;

    ...
}

这阻碍了它的工作。要解决这个问题,你需要修改你的NuGet。然后删除“OutputDirectory”参数:

    <RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)"  $(RequireConsentSwitch)</RestoreCommand>

所以现在,如果你在NuGet中添加'repositoryPath'配置。Config(参见发布说明中关于放置配置文件的有效位置的描述),它将把所有包恢复到单个位置,但是…您的.csproj仍然包含以相对路径编写的程序集提示…

我仍然不明白为什么他们走了艰难的道路,而不是改变PackageManager,这样它就会添加相对于PackagesDir的提示路径。这就是我手动在本地(在我的桌面上)和构建代理上拥有不同包位置的方法。

<Reference Include="Autofac.Configuration, Version=2.6.3.862, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
  <Private>True</Private>
  <HintPath>$(PackagesDir)\Autofac.2.6.3.862\lib\NET40\Autofac.Configuration.dll</HintPath>
</Reference>

其他回答

创建一个名为“nuget.config”的文件。 把那个文件加到我的解决方案文件夹里了

这对我不起作用:

<configuration>
  <config>
    <add key="repositoryPath" value="..\ExtLibs\Packages" />
  </config>
  ... 
</configuration>

这对我来说确实有用:

<?xml version="1.0" encoding="utf-8"?>
<settings>
  <repositoryPath>..\ExtLibs\Packages</repositoryPath>
</settings>

刚刚更新了Nuget 2.8.3。要更改已安装包的位置,我启用了从右键单击解决方案恢复包。NuGet编辑。配置并添加这些行:

  <config>
    <add key="repositorypath" value="..\Core\Packages" />
  </config>

然后重新构建解决方案,它将所有包下载到我想要的文件夹,并自动更新引用。

为了改变项目的路径使用PackageReference而不是包。配置你需要使用globalPackagesFolder

从https://learn.microsoft.com/en-us/nuget/reference/nuget-config-file

globalPackagesFolder (projects using PackageReference only) The location of the default global packages folder. The default is %userprofile%.nuget\packages (Windows) or ~/.nuget/packages (Mac/Linux). A relative path can be used in project-specific nuget.config files. This setting is overridden by the NUGET_PACKAGES environment variable, which takes precedence. repositoryPath (packages.config only) The location in which to install NuGet packages instead of the default $(Solutiondir)/packages folder. A relative path can be used in project-specific nuget.config files. This setting is overridden by the NUGET_PACKAGES environment variable, which takes precedence.

<config>
    <add key="globalPackagesFolder" value="c:\packageReferences" />
    <add key="repositoryPath" value="c:\packagesConfig" />
</config>

我放了Nuget。配置旁边的解决方案文件,它工作。

除了Shane Kms的答案,如果你已经激活Nuget包还原,你编辑Nuget。在.nuget文件夹中的配置如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <repositoryPath>..\..\ExtLibs\Packages</repositoryPath>
</configuration>

注意多余的“..”“\”,因为它从.nuget文件夹而不是解决方案文件夹返回。

在使用Visual Studio 2019和NuGet v4或更高版本时,我正在寻找一种不涉及更改机器级或用户级配置,也不使用绝对路径的解决方案。

根据NuGet文档,该值可以被NuGet覆盖。配置文件放置在当前文件夹(例如解决方案文件夹)或任何文件夹到驱动器根。请注意,NuGet 3.3和更早的版本期待NuGet。配置文件要放在.nuget子文件夹中。这一开始让我很困扰,因为我的设置最初针对的是旧版本,所以我删除了这个子文件夹。

我最后得到了一块鸡块。配置文件放在我的Dev文件夹(其中包含不同VS解决方案的子文件夹):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <config>
    <add key="repositoryPath" value=".\installed_packages" />
    <add key="globalPackagesFolder" value=".\installed_packages" />
  </config>
</configuration>

通过这个设置,我的Dev文件夹下的所有解决方案/项目都将它们的包安装在名为installed_packages的文件夹中,位于与nuget相同的级别。配置文件。

最后,请注意repositoryPath是由使用包的项目使用的。而globalPackagesFolder是由使用PackageReference的项目使用的。