管理全域套件、快取和暫存資料夾Managing the global packages, cache, and temp folders

當您安裝、更新或還原套件時,NuGet 會管理專案結構之外數個資料夾中的套件和套件資訊:Whenever you install, update, or restore a package, NuGet manages packages and package information in several folders outside of your project structure:

名稱Name 描述和位置 (依使用者)Description and Location (per user)
global‑packagesglobal‑packages global-packages 資料夾是 NuGet 安裝下載套件的位置。The global-packages folder is where NuGet installs any downloaded package. 每個套件已經完全展開至符合套件識別碼和版本號碼的子資料夾。Each package is fully expanded into a subfolder that matches the package identifier and version number. 使用 PackageReference 格式的專案一律使用直接從此資料夾取得的套件。Projects using the PackageReference format always use packages directly from this folder. 當使用 packages.config 時,套件會安裝到 global-packages 資料夾,然後複製到專案的 packages 資料夾。When using the packages.config, packages are installed to the global-packages folder, then copied into the project's packages folder.
  • Windows:%userprofile%\.nuget\packagesWindows: %userprofile%\.nuget\packages
  • Mac/Linux:~/.nuget/packagesMac/Linux: ~/.nuget/packages
  • 使用 NUGET_PACKAGES 環境變數、globalPackagesFolderrepositoryPath 組態設定 (當分別使用 PackageReference 和 packages.config 時),或 RestorePackagesPath MSBuild 屬性 (僅限 MSBuild) 來覆寫。Override using the NUGET_PACKAGES environment variable, the globalPackagesFolder or repositoryPath configuration settings (when using PackageReference and packages.config, respectively), or the RestorePackagesPath MSBuild property (MSBuild only). 環境變數的優先性高於組態設定。The environment variable takes precedence over the configuration setting.
http‑cachehttp‑cache Visual Studio 套件管理員 (NuGet 3.x+) 和 dotnet 工具會將下載的套件複本存放在此快取中 (另存為 .dat 檔案),並組織到每個套件來源的子資料夾。The Visual Studio Package Manager (NuGet 3.x+) and the dotnet tool store copies of downloaded packages in this cache (saved as .dat files), organized into subfolders for each package source. 套件不會展開,而且快取的到期時間為 30 分鐘。Packages are not expanded, and the cache has an expiration time of 30 minutes.
  • Windows:%localappdata%\NuGet\v3-cacheWindows: %localappdata%\NuGet\v3-cache
  • Mac/Linux:~/.local/share/NuGet/v3-cacheMac/Linux: ~/.local/share/NuGet/v3-cache
  • 使用 NUGET_HTTP_CACHE_PATH 環境變數來覆寫。Override using the NUGET_HTTP_CACHE_PATH environment variable.
temptemp NuGet 在其各種作業期間存放暫存檔的資料夾。A folder where NuGet stores temporary files during its various operations.
  • Windows:%temp%\NuGetScratchWindows: %temp%\NuGetScratch
  • Mac/Linux:/tmp/NuGetScratchMac/Linux: /tmp/NuGetScratch
  • plugins-cache 4.8+plugins-cache 4.8+ NuGet 在此資料夾中儲存來自作業宣告要求的結果。A folder where NuGet stores the results from the operation claims request.
    • Windows:%localappdata%\NuGet\plugins-cacheWindows: %localappdata%\NuGet\plugins-cache
    • Mac/Linux:~/.local/share/NuGet/plugins-cacheMac/Linux: ~/.local/share/NuGet/plugins-cache
    • 使用 NUGET_PLUGINS_CACHE_PATH 環境變數來覆寫。Override using the NUGET_PLUGINS_CACHE_PATH environment variable.

    注意

    NuGet 3.5 和更早版本使用的是 packages-cache,而非位於 %localappdata%\NuGet\Cache 中的 http-cacheNuGet 3.5 and earlier uses packages-cache instead of the http-cache, which is located in %localappdata%\NuGet\Cache.

    NuGet 通常可使用快取和 global-packages 資料夾,以避免下載已經存在於電腦上的套件,藉此改善安裝、更新及還原作業的效能。By using the cache and global-packages folders, NuGet generally avoids downloading packages that already exist on the computer, improving the performance of install, update, and restore operations. 當使用 PackageReference 時,global-packages 資料夾也可避免將下載的套件保留在專案資料夾內 (以免可能會不小心新增到原始檔控制中),並降低 NuGet 對電腦儲存體的整體影響。When using PackageReference, the global-packages folder also avoids keeping downloaded packages inside project folders, where they might be inadvertently added to source control, and reduces NuGet's overall impact on computer storage.

    NuGet 需要擷取套件時,首先會尋找 global-packages 資料夾。When asked to retrieve a package, NuGet first looks in the global-packages folder. 如果套件的正確版本不存在,NuGet 便會檢查所有非 HTTP 套件來源。If the exact version of package is not there, then NuGet checks all non-HTTP package sources. 如果仍然找不到套件,除非您使用 dotnet.exe 命令指定 --no-cache或使用 nuget.exe 命令指定 -NoCache,否則 NuGet 會尋找 http-cache 中的套件。If the package is still not found, NuGet looks for the package in the http-cache unless you specify --no-cache with dotnet.exe commands or -NoCache with nuget.exe commands. 如果套件不在快取中,或者未使用快取,則 NuGet 會透過 HTTP 擷取套件。If the package is not in the cache, or the cache isn't used, NuGet then retrieves the package over HTTP .

    如需詳細資訊,請參閱安裝套件後會發生什麼事For more information, see What happens when a package is installed?.

    檢視資料夾位置Viewing folder locations

    您可以使用 nuget locals 命令來檢視位置:You can view locations using the nuget locals command:

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

    一般輸出 (Windows;"user1" 是目前使用者名稱):Typical output (Windows; "user1" is the current username):

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

    (package-cache 使用於 NuGet 2.x 中,並在 NuGet 3.5 及更早版本中出現)。(package-cache is used in NuGet 2.x and appears with NuGet 3.5 and earlier.)

    您可以使用 dotnet nuget locals 命令來檢視資料夾位置:You can also view folder locations using the dotnet nuget locals command:

    dotnet nuget locals all --list
    

    一般輸出 (Mac/Linux;"user1" 是目前使用者名稱):Typical output (Mac/Linux; "user1" is the current username):

    info : http-cache: /home/user1/.local/share/NuGet/v3-cache
    info : global-packages: /home/user1/.nuget/packages/
    info : temp: /tmp/NuGetScratch
    info : plugins-cache: /home/user1/.local/share/NuGet/plugins-cache
    

    若要顯示單一資料夾的位置,請使用 http-cacheglobal-packagestempplugins-cache,不要使用 allTo display the location of a single folder, use http-cache, global-packages, temp, or plugins-cache instead of all.

    清除本機資料夾Clearing local folders

    如果遇到套件安裝問題,或想要確保安裝的是遠端資源庫的套件,請使用 locals --clear 選項 (dotnet.exe) 或 locals -clear (nuget.exe),指定要清除的資料夾,或 all 以清除所有資料夾:If you encounter package installation problems or otherwise want to ensure that you're installing packages from a remote gallery, use the locals --clear option (dotnet.exe) or locals -clear (nuget.exe), specifying the folder to clear, or all to clear all folders:

    # Clear the 3.x+ cache (use either command)
    dotnet nuget locals http-cache --clear
    nuget locals http-cache -clear
    
    # Clear the 2.x cache (NuGet CLI 3.5 and earlier only)
    nuget locals packages-cache -clear
    
    # Clear the global packages folder (use either command)
    dotnet nuget locals global-packages --clear
    nuget locals global-packages -clear
    
    # Clear the temporary cache (use either command)
    dotnet nuget locals temp --clear
    nuget locals temp -clear
    
    # Clear the plugins cache (use either command)
    dotnet nuget locals plugins-cache --clear
    nuget locals plugins-cache -clear
    
    # Clear all caches (use either command)
    dotnet nuget locals all --clear
    nuget locals all -clear
    

    目前在 Visual Studio 中開啟且由專案使用的任何套件不會從 global-packages 資料夾清除。Any packages used by projects that are currently open in Visual Studio are not cleared from the global-packages folder.

    在 Visual Studio 2017 中啟動,使用 [工具] > [NuGet 套件管理員] > [套件管理員設定]**** 功能表命令,然後選取 [清除所有 NuGet 快取]****。Starting in Visual Studio 2017, use the Tools > NuGet Package Manager > Package Manager Settings menu command, then select Clear All NuGet Cache(s). 目前無法透過套件管理員主控台來管理快取。Managing the cache isn't presently available through the Package Manager Console. 在 Visual Studio 2015 中,請改用 CLI 命令。In Visual Studio 2015, use the CLI commands instead.

    用來清除快取的 NuGet 選項命令

    疑難排解錯誤Troubleshooting errors

    使用 nuget localsdotnet nuget locals 時,可能會發生下列錯誤:The following errors can occur when using nuget locals or dotnet nuget locals:

    • 錯誤:「處理序無法存取檔案 因為其他處理序正在使用它」,或者「清除本機資源失敗:無法刪除一或多個檔案」Error: The process cannot access the file because it is being used by another process or Clearing local resources failed: Unable to delete one or more files

      其他處理序正在使用資料夾內的一或多個檔案;例如,Visual Studio 專案已開啟,其中參照 global-packages 資料夾中的套件。One or more files in the folder are in use by another process; for example, a Visual Studio project is open that refers to packages in the global-packages folder. 關閉這些處理序,並再試一次。Close those processes and try again.

    • 錯誤:「存取路徑 遭到拒絕」** 或者「目錄不是空的」**Error: Access to the path is denied or The directory is not empty

      您沒有刪除此快取中檔案的權限。You don't have permission to delete files in the cache. 如果可能,請變更資料夾權限,並再試一次。Change the folder permissions, if possible, and try again. 否則,請洽詢系統管理員。Otherwise, contact your system administrator.

    • 錯誤:指定的路徑、檔名或兩者都太長。完全限定的檔名必須小於 260 個字元,目錄名稱必須小於 248 個字元。Error: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

      請縮短資料夾名稱,並再試一次。Shorten the folder names and try again.