3

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)?

Michael
  • 548
  • 6
  • 23
  • what's the purpose of doing this? why not just add the same nuget package into your new project ? – LeY Nov 27 '18 at 17:36
  • @Ley, I have a Git repo with multiple projects that have different dependencies and I ignore the packages folder, I rely on the 'Restore nuget packages' option which needs to be able to located those packages. They are already downloaded on my PC, I just need to inform the project that I'm working with about it. – Michael Nov 27 '18 at 17:40
  • I'm by no means an expert, maybe my way of structuring the repo is awful. Let me know if it is and if there is a better option. But the goal of my repo is to have multiple folders with different projects that do different things, have different dependencies and can be referenced by each other in some solution (as projects). – Michael Nov 27 '18 at 17:44
  • what you described is basically the default behaviors of Visual studio's Nuget package manager, all your dll is downloaded into ..\packages which will be shared by all project that referencing it. once you install the package using Nuget pacage manage, it will add the reference to your project automatically and create a packages.config file to your project folder which indicate what nuget package this project use, and when your restore it , it will found it correctly. – LeY Nov 27 '18 at 17:59

1 Answers1

6

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>

enter image description here

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.

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • Yes, this is exactly what I needed, thank you for sharing this information. I didn't know you could use 'Package resourses' in such a way. – Michael Nov 30 '18 at 18:30