2

I hava a solution which contains two Project.

/Solution
    .sln
    .nuget.config
    /ProjectA
        .csproj
        .nuspec
    /ProejctB
        .csproj
        .nuspec

When I add a dependency ProjectA via nuget.

enter image description here

Project structure is become something like this:

/Solution
    .sln
    nuget.config
    /packages
         .nupkg
         /lib
             .dll
    /ProjectA
        .csproj
        .nuspec
        packages.config
    /ProejctB
        .csproj
        .nuspec

Basically, Visual Studio create a packages.config file and put dependency there. And, dependency is added to /packages folder. Also, .csproj content changes.(A Reference added which has a relative path to dll inside /packages folder.)

I have two question.

1- Inside nuget.config file I have put following line. So I expect nuget will use default local repository instead of creating a /packages folder in solution. How can I prevent creation of a /packages folder in solution. Instead I would like to use "%userprofile%\.nuget\packages".

<add key="globalPackagesFolder" value="%userprofile%\.nuget\packages" />

2- When I add the dependency as nuget package. Still .csprj file is changed and dll's related path is added there as a reference with relative path. Is it ok? Isn't auto generated packages.config enough. Why also this file is updated.

clockworks
  • 3,755
  • 5
  • 37
  • 46
  • Why does it matter that there is a packages folder inside the solution? – Blue Jul 18 '17 at 04:57
  • Instead having a package folder in each solution, I think it is better to have one package folder in a system and each solution may use this folder. "%userprofile%\.nuget\packages" already exists so I would like to use it. – clockworks Jul 18 '17 at 05:17
  • The package manager uses a local cache. It will copy the dlls into each directory (As different projects may reference different versions) – Blue Jul 18 '17 at 05:38
  • @melih.tt, I have update my answer more detail info, you can check if it works for you. If not, please let me know the latest status after using below method. – Leo Liu Jul 19 '17 at 06:25

1 Answers1

5

1- Inside nuget.config file I have put following line. So I expect nuget will use default local repository instead of creating a /packages folder in solution. How can I prevent creation of a /packages folder in solution. Instead I would like to use "%userprofile%.nuget\packages".

You should use below settings in the NuGet.Config:

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

You can check the test result from below screenshot, the packages is add to the "%userprofile%\.nuget\packages" folder:

enter image description here

Note: Take care the uppercase and lowercase for the file name and restart the VS after add it.

2- When I add the dependency as nuget package. Still .csprj file is changed and dll's related path is added there as a reference with relative path. Is it ok? Isn't auto generated packages.config enough. Why also this file is updated.

Not, because the Package.config and the HintPath in the .csproj file have different role. The Package.config is used by NuGet to manage the Packages, and the HintPath in the .csproj is used by the Visual Studio reference the path of the Dll file. Both files are need to be updated, otherwise Visual Studio will throw the error "Can not find the reference..."

Leo Liu
  • 71,098
  • 10
  • 114
  • 135