Net Core 默认全局。因受到网络防火墙,现需要在将包放项目当前目录,以便在不同网络环境迁移时构建。牺牲专案目录大小。
在当前目录建NuGet.config
然后执行dotnet restore 执行还原下载
In modern .NET Core projects using the PackageReference format, NuGet packages are not stored in a project-specific or solution-specific "packages" folder by default. Instead, they are downloaded and stored in a global-packages folder that is shared by all projects on the machine. Microsoft Learn +2
Default Global Packages Folder Location
The default location for the global packages folder depends on the operating system:
- Windows:
%userprofile%\.nuget\packages - macOS/Linux:
~/.nuget/packagesStack Overflow +3
Each package is expanded into a subfolder matching its identifier and version number within this global folder. Microsoft Learn
Configuring a Project-Specific Packages Folder
While the global cache is the default and recommended approach (saving significant disk space across multiple projects/solutions), you can force a single project (or solution) to use a local, dedicated packages folder by using a NuGet.config file. Stack Overflow +1
To set this up for a single project:
-
Create a
NuGet.configfile in the same directory as your project's.csprojfile. -
Add the
globalPackagesFolderkey within the<config>section of theNuGet.configfile, specifying a relative or absolute path for the folder.xml
<?xml version="1.0" encoding="utf-8"?> <configuration> <config> <!-- The ".\packages" value will create a folder named 'packages' relative to the config file location --> <add key="globalPackagesFolder" value=".\packages" /> </config> <!-- You might also need to explicitly define package sources if not using nuget.org default --> <packageSources> <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" /> </packageSources> </configuration> -
Run
dotnet restoreor build your project (or use the Visual Studio package manager features). The packages will now be downloaded to the specified local folder. -
(Optional, for source control): If you wish to commit these packages to source control (which is generally discouraged but was a common practice with older
.NET Frameworkprojects), make sure your.gitignoreor equivalent file does not exclude the specifiedpackagesfolder.Stack Overflow +4
For more details on managing NuGet configuration, refer to the official Microsoft Learn documentation.