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

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

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


当前回答

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

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

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

其他回答

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

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



    

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

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>

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

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。在解决方案文件所在目录下配置,包含以下内容:

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

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

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