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

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

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


当前回答

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

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

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

其他回答

VS 2017更新:

看起来人们在Nuget团队终于开始使用Nuget自己,帮助他们找到和修复几个重要的东西。所以现在(如果我没有弄错的话,因为还没有迁移到VS 2017)下面的内容不再需要了。您应该能够将“repositoryPath”设置为本地文件夹,并且它将工作。甚至可以将其保留为默认恢复位置,从解决方案文件夹移到机器级。我还是没有自己测试

VS 2015及更早

只是给其他答案一个提示(特别是这个):

NuGet包文件夹的位置可以通过配置来更改,但是VisualStudio仍然相对地引用此文件夹中的程序集:

<HintPath>..\..\..\..\..\..\SomeAssembly\lib\net45\SomeAssembly.dll</HintPath>

为了解决这个问题(直到有更好的解决方案),我使用subst命令创建了一个指向Packages文件夹新位置的虚拟驱动器:

subst N: C:\Development\NuGet\Packages

现在当添加一个新的NuGet包时,项目引用使用它的绝对位置:

<HintPath>N:\SomeAssembly\lib\net45\SomeAssembly.dll</HintPath>

注意:

这样的虚拟驱动器将在重新启动后删除,因此请确保您处理了它 不要忘记替换项目文件中现有的引用。

最一致的方法是使用nuget config显式设置配置:

nuget config -set repositoryPath=c:\packages -configfile c:\my.config

https://learn.microsoft.com/en-us/nuget/consume-packages/configuring-nuget-behavior#changing-config-settings

好吧,为了阅读这篇文章的其他人,以下是我对上述无数答案的理解:

The nuget.config file in the .nuget folder is relative to that folder. This is important because if your new folder is something like '../Packages' that will put it where it always goes out of the box. As @bruce14 states you must do '../../Packages' instead I could not get the latest nuget (2.8.5) to find a packages folder outside of the standard location without enabling package restore. So once you enable package restore then the following should be added to the nuget.config file inside of the .nuget folder to change the location: <?xml version="1.0" encoding="utf-8"?> <configuration> ... <config> <add key="repositoryPath" value="..\..\Packages" /> </config> ... </configuration> (This is important) If you make ANY changes to the package folder location inside of the nuget.config files you must restart visual studio or close/reload the solution for the changes to take effect

如果您使用的是Visual Studio 2019和NuGet版本4(或更高版本),您可以通过编辑NuGet来更改路径。配置文件。

NuGet。配置文件在“C:\Users%USER_NAME%\AppData\Roaming\NuGet”目录下

在配置文件中增加“globalPackagesFolder”和“repositoryPath”键。设置您想要的值。

这里有一个例子

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="Microsoft Visual Studio Offline Packages" value="C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\" />
  </packageSources>
  <config>
    <add key="globalPackagesFolder" value="E:\.packages" />
    <add key="repositoryPath" value="E:\.nuget" />
  </config>
</configuration>

创造一个金块。在解决方案文件所在目录中的配置文件中输入以下内容,并重新启动visual studio。(我用VS2022测试它)。/packages -用你的包路径替换它]

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <settings>
  <repositoryPath>./packages</repositoryPath>
 </settings>
</configuration>