yanbolan
文章282
標籤199
類別26
nuget当前packages目录

nuget当前packages目录


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 LearnMicrosoft 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/packages Stack OverflowStack Overflow +3

Each package is expanded into a subfolder matching its identifier and version number within this global folder. Microsoft LearnMicrosoft 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 OverflowStack Overflow +1

To set this up for a single project:

  1. Create a NuGet.config file in the same directory as your project’s .csproj file.

  2. Add the globalPackagesFolder key within the <config> section of the NuGet.config file, specifying a relative or absolute path for the folder.

    xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?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>

  3. Run dotnet restore or build your project (or use the Visual Studio package manager features). The packages will now be downloaded to the specified local folder.

  4. (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 Framework projects), make sure your .gitignore or equivalent file does not exclude the specified packages folder. Stack OverflowStack Overflow +4

For more details on managing NuGet configuration, refer to the official Microsoft Learn documentation.

博主:yanbolan
文章連結:https://yuris.site/2026/03_04_133a0cf6.html
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可
×