简体   繁体   中英

Change nuget packages search&install folder for a project in Visual Studio 2017

I've created a solution with one project in VS2017. It is a .Net C# Winforms Application. I want the project to load in the nuget packages that have been downloaded earlier by other projects into a certain folder on my computer and add them to its references.

The packages folder is located one directory back from the solution directory.

How could I achieve this?

I've already tried adding a nuget.config file to the solution location that contains the following XML code:

<?xml version="1.0" encoding="utf-8"?>

<configuration>

  <config>
    <add key="globalPackagesFolder" value="..\packages" />
    <add key="repositoryPath" value="..\packages" />
  </config>

</configuration>

My solution seemed to ignore it completely even after including it in the VS solution explorer.

I've also read that globalPackagesFolder is overridden by NUGET_PACKAGES (or something along those lines) environmental variable, so what is the point of setting it in the .config file? Or am I not understanding something correctly?

Also, a little bit off topic, but still, I am really curious. When I click 'Manage nuget packages' on my project and download something those packages are placed one directory before the .csproj file of my project. Can I change where these packages for that particular project are being downloaded to (and pulled from into the references of my project)?

Change nuget packages search&install folder for a project in Visual Studio 2017

To answer this question, we need to be clear about two concepts, the Package source and Repository . Obviously, the Package source is used to download the nuget packages, and the Repository is used to store the nuget packages for the solution. In simple terms, Nuget management package is download the package from package source , add package to the project and store the nuget packages in the Repository .

So, if you want the Winforms Application to load those packages that have been downloaded earlier by other projects into a certain folder and add them to its references, you have two things to do (If I have understand you correctly).

One is add those packages that have been downloaded earlier by other projects to your Winforms Application, another is add those packages into a certain folder.

To resolve the first thing, it need to downloaded package, so we have to make the folder where store nuget packages that downloaded earlier by other projects to the package source. Go to options->Tools->NuGet Package Manager->Package Sources, add the folder where stored the packages that downloaded earlier by other projects, then you can add those packages to the Winforms Application via nuget package manager UI:

Check this thread: Installing NuGet package located in local package repository into a new Visual Studio Solution

To resolve the second question, just like what you did, adding a nuget.config file to the solution location that contains the following XML code:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <config>
    <add key="repositoryPath" value="..\packages" />
  </config>

</configuration>

在此处输入图片说明

No need for the setting for globalPackagesFolder , that is used for the global Packages Folder, C:\\Users\\<UserName>\\.nuget\\packages .

Note: After adding the nuget.config , remember to restart the Visual Studio, then add the nuget packages, the packages will store in the directory before the .sl file of my solution(Since you set the repositoryPath as ..\\packages ).

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM