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

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

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


当前回答

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

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

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

其他回答

为了改变项目的路径使用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。配置旁边的解决方案文件,它工作。

我又发现了一个小秘密。(这可能是最基本的,有些人还没有提到,但它对我的解决方案很重要。)“packages”文件夹最终与你的.sln文件在同一个文件夹中。

我们移动了.sln文件,然后固定了其中的所有路径,以找到各种项目,瞧!我们的packages文件夹最终位于我们想要的位置。

在已接受的答案中的配置文件在VS2012中为我工作。 然而,对我来说,只有当我做到以下几点时,它才会起作用:

在VS中创建一个新项目。 退出VS -这似乎很重要。 将配置文件复制到项目文件夹。 重新启动VS并添加包。

如果我遵循这些步骤,我就可以使用共享包文件夹。

对于.NET Core项目和Visual Studio 2017,我能够通过提供以下配置将所有包恢复到相对路径:

<configuration>
  <config>
    <add key="globalPackagesFolder" value="lib" />
  </config>
  ... 
</configuration>

根据我的经验,lib文件夹是在Nuget的同一级别上创建的。无论SLN文件在哪里,都可以找到配置。 我测试了,命令行dotnet恢复和Visual Studio 2017重建的行为是相同的

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

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

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