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

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

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


现在可以控制将包安装到哪个文件夹。

http://nuget.codeplex.com/workitem/215

编辑: 请看Phil Haack在2010年12月10日晚上11:45的评论(在工作项/上面的链接中)。该支持在1.0中部分实现,但没有文档记录。

@dfowler表示: 加一块鸡块。配置文件旁边的解决方案与此:

<settings>
<repositoryPath>{some path here}</repositoryPath>
</settings>

有一个用于创建包文件夹覆盖的nuget包。

版本2.1的更新

正如阿扎特所评论的,现在有关于如何控制包裹位置的官方文件。2.1版本的发布说明在一个nuget中指定了以下配置。配置文件(关于放置配置文件的有效位置以及分层配置模型如何工作的描述,请参阅发布说明):

<configuration>
  <config>
    <add key="repositoryPath" value="C:\thePathToMyPackagesFolder" />
  </config>
  ... 
</configuration>

这将更改您放入文件的配置级别的packages文件夹(解决方案,如果您将其放在解决方案目录中,项目在项目目录中等等)。注意,发布说明声明:

[…如果您的解决方案下面有一个现有的packages文件夹 在NuGet放置包之前,你需要删除它 新地点。

创建一个名为“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>

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>

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

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

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

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

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

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

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

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

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

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

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

Visual Studio 2015上Nuget 3.2的解决方案是:

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

为父文件夹使用正斜杠。 将上述文件(nuget.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 2.8.6),因为缺少一些提示,我会尝试在这里添加它们,因为它可能对其他人有用。

阅读以下资料后: https://docs.nuget.org/consume/NuGet-Config-Settings https://github.com/NuGet/Home/issues/1346 看来

To make working Install-Package properly with different repositoryPath you need to use forward slashes, it's because they are using Uri object to parse location. Without $ on the begining it was still ignoring my settings. NuGet caches config file, so after modifications you need to reload solution/VS. I had also strange issue while using command of NuGet.exe to set this option, as it modified my global NuGet.exe under AppData\Roaming\NuGet and started to restore packages there (Since that file has higher priority, just guessing).

如。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <solution>
    <add key="disableSourceControlIntegration" value="true" />
  </solution>
  <config>
    <add key="repositorypath" value="$/../../../Common/packages" />
  </config>
</configuration>

你也可以使用NuGet命令来确保语法是正确的,就像这样:

NuGet.exe config -Set repositoryPath=$/../../../Common/packages -ConfigFile NuGet.Config

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

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

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

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

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

最一致的方法是使用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

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

创建nuget。在解决方案文件所在目录下配置,包含以下内容:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <config>
    <add key="repositoryPath" value="packages" />
  </config>
</configuration>

'packages'将是还原所有包的文件夹。

关闭Visual studio解决方案并重新打开。

如果您使用的是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 2019和Nuget 5.9

打开%AppData%\NuGet文件夹,打开现有的NuGet。配置文件。编辑repositoryPath键并设置新的目标。

<配置> <add key=“存储路径”值=“D:\”鸡块\ packages”- > < /配置>

在环境变量中编辑系统变量NUGET_PACKAGES并设置新的目标

重启VS.不需要放金币。配置文件在每个解决方案。

在使用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的项目使用的。

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

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