受欢迎的博客标签

how to Setting the NuGet package folder location for Visual Studio solutions

Published

Is it possible to change the location of C:\Users\Myaccount\.nuget folder to another folder in drive D: ?

I need to empty some space for my C: drive.

 

 

The default folder where C# solutions will save the nuget packages is %UserProfile%\.nuget\packages .

To change to different one there are 2 options:

Add config to NuGet.config file
or
Adding NUGET_PACKAGES env variable

 

Listing global packages,Viewing folder locations

https://learn.microsoft.com/en-us/nuget/consume-packages/managing-the-global-packages-and-cache-folders#viewing-folder-locations

# Display locals for all folders: global-packages, http cache, temp and plugins cache
dotnet nuget locals all --list

output

http-cache: C:\Users\Administrator\AppData\Local\NuGet\v3-cache
global-packages: C:\Users\Administrator\.nuget\packages\
temp: C:\Users\Administrator\AppData\Local\Temp\NuGetScratch
plugins-cache: C:\Users\Administrator\AppData\Local\NuGet\plugins-cache

 

There are multiple nuget packages read in the following order:

First the NuGetDefaults.Config file. You will find this in %ProgramFiles(x86)%\NuGet\Config.
The computer-level file.
The user-level file. You will find this in %APPDATA%\NuGet\nuget.config.
Any file named nuget.config beginning from the root of your drive up to the directory where nuget.exe is called.
The config file you specify in the -configfile option when calling nuget.exe

 

https://siderite.dev/blog/setting-nuget-package-folder-location.html/

There are two ways of configuring NuGet packages for your projects:

a packages.config file 

PackageReference elements in your .csproj file

 

There are two locations for the nuget.config file: in a .nuget folder inside your solution folder and directly in the solution folder (or any of its parent folders)
The location that is accepted by the latest versions of NuGet is directly in the solution folder
Sometimes you need to restart Visual Studio for the change in nuget.config files to considered
The path you specify is relative to the nuget.config file, no matter where it is

By default, Visual Studio uses global Nuget.config located at path C:\Users\yourusername\AppData\Roaming\NuGet\Nuget.config

change the location of C:\Users\Myaccount\.nuget folder to another folder in drive D:

f you want to change the location of NuGet packages from drive C to D, please try the following:

option 1. Using the NUGET_PACKAGES environment variable:

NUGET_PACKAGES = D:\xxx
NUGET_PACKAGES = F:\tmp\NuGetPackages

 

or

 

option 2. Please add the following configuration in a nuget.config file(C:\Users\xxx\AppData\Roaming\NuGet):

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

https://learn.microsoft.com/en-us/nuget/reference/nuget-config-file#config-section

 

 you will have to edit the Nuget.config file.

In Windows 10, this file is in the path:
C:\Users\YouUser\AppData\Roaming\NuGet.

C:\Users\Administrator\AppData\Roaming\NuGet\nuget.config

 

 

 

old

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
  </packageSources>
</configuration>

new

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <config>
    <add key="globalPackagesFolder" value="F:\tmp\NuGetPackages" />
  </config>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
  </packageSources>
</configuration>

https://learn.microsoft.com/zh-cn/nuget/reference/nuget-config-file

Managing the global packages, cache, and temp folders

https://learn.microsoft.com/en-us/nuget/consume-packages/managing-the-global-packages-and-cache-folders

global-packages Windows: %userprofile%\.nuget\packages
temp                   Windows: %temp%\NuGetScratch

Clearing local folders

# Clear all caches (use either command)
dotnet nuget locals all --clear
nuget locals all -clear

 

Setting the NuGet package folder location for Visual Studio solutions

https://siderite.dev/blog/setting-nuget-package-folder-location.html/

https://dev.to/tombohub/how-to-change-default-nuget-packages-folder-on-windows-51hb

https://github.com/NuGet/Home/issues/4810