(不包含在 Visual Studio 用于 Mac 或 Visual Studio Code。)

使用控制台中的 CLI nuget.exe。

例如,查找和安装的包,可使用三个简单步骤:

  1. 在 Visual Studio 中,打开项目/解决方案并打开控制台使用工具 > NuGet 包管理器 > 程序包管理器控制台命令。

  2. 如果你已经知道此,请跳到步骤 3。

    ps
    # Find packages containing the keyword "elmah"
    Find-Package elmah
    
  3. 运行安装命令:

    ps
    # Install the Elmah package to the project named MyProject.
    Install-Package Elmah -ProjectName MyProject
    

重要

为此,通常在 Visual Studio 中工作的开发人员喜欢使用 CLI 到控制台。

提示

保存未保存的解决方案,或创建和保存解决方案,如果你还没有打开,应纠正该错误。

打开的控制台和控制台控件

  1. 自定义 Visual Studio 中的窗口布局)。

  2. 默认情况下,控制台命令运行针对特定的包源和项目中控件的窗口的顶部设置:

    C# NuGet包管理命令

  3. 覆盖而无需更改默认设置,这些设置的大多数命令支持-Source-ProjectName选项。

  4. 此外,右侧为项目选择器控件清除控制台的内容:

    C# NuGet包管理命令

  5. 例如,运行Get-Package -ListAvailable -PageSize 500列出上默认源 (如 nuget.org),可能需要几分钟时间运行的前 500 包。

    C# NuGet包管理命令

安装的包

ps
# Add the Elmah package to the default project as specified in the console's project selector
Install-Package Elmah

# Add the Elmah package to a project named UtilitiesLib that is not the default
Install-Package Elmah -ProjectName UtilitiesLib

安装包。

安装的包执行下列操作:

  • 如果你不同意这些条款,你应立即卸载程序包。
  • 但是,请注意,采用 PackageReference,则需要先保存该项目才能直接看到项目文件中的更改。
  • 缓存包:
    • PackageReference: 在缓存包%USERPROFILE%\.nuget\packages和锁定文件即project.assets.json更新。
    • package.config更新文件。
  • 源和配置文件转换。
  • 依赖项解析。
  • 如果可用,请在 Visual Studio 窗口中显示包的自述文件。

提示

与此相反, nuget install CLI 命令仅下载包,并不会自动添加引用。

卸载包

ps
# Uninstalls the Elmah package from the default project
Uninstall-Package Elmah

# Uninstalls the Elmah package and all its unused dependencies
Uninstall-Package Elmah -RemoveDependencies 

# Uninstalls the Elmah package even if another package depends on it
Uninstall-Package Elmah -Force

Get 包查看当前安装在默认项目中,如果你需要查找标识符的所有包。

卸载程序包执行下列操作:

  • (你可能需要重新生成该项目才能看到它从删除Bin文件夹。)
  • 反转对所做任何更改app.configweb.config时已安装了包。
  • 如果没有剩余的包使用这些依赖关系,依赖以前安装中删除项。

提示

Install-PackageUninstall-Package命令具有与管理在项目中,引用的好处nuget uninstallCLI 命令。

更新程序包

ps
# Checks if there are newer versions available for any installed packages
Get-Package -updates

# Updates a specific package using its identifier, in this case jQuery
Update-Package jQuery

# Update all packages in the project named MyProject (as it appears in Solution Explorer)
Update-Package -ProjectName MyProject

# Update all packages in the solution
Update-Package

更新包

查找包

ps
# Find packages containing keywords
Find-Package elmah
Find-Package logging

# List packages whose ID begins with Elmah
Find-Package Elmah -StartWith

# By default, Get-Package returns a list of 20 packages; use -First to show more
Find-Package logging -First 100

# List all versions of the package with the ID of "jquery"
Find-Package jquery -AllVersions -ExactMatch

Get 包相反。

控制台可用性

在 Visual Studio 2017,NuGet 和 NuGet 包管理器将自动安装时选择任何。提供与.NET 相关的工作负荷;你就可以还单独安装它,通过检查各个组件 > 代码工具 > NuGet 包管理器在 Visual Studio 2017 安装程序中的选项。

https://dist.nuget.org/index.html。

中你的项目包括 NuGet 包。

程序包管理器控制台不包括 Visual Studio 代码。

扩展包管理器控制台

例如,MvcScaffolding创建等命令Scaffold下面所示,这将生成 ASP.NET MVC 控制器和视图:

C# NuGet包管理命令

设置 NuGet PowerShell 配置文件

NuGet 支持通常在以下位置找到 NuGet 特定配置文件:

%UserProfile%\Documents\WindowsPowerShell\NuGet_profile.ps1

若要查找配置文件,请键入$profile在控制台中:

ps
$profile
C:\Users\<user>\Documents\WindowsPowerShell\NuGet_profile.ps1

Windows PowerShell 配置文件。

使用 nuget.exe CLI 在控制台中

NuGet.CommandLine从控制台的包:

ps
# Other versions are available, see http://www.nuget.org/packages/NuGet.CommandLine/
Install-Package NuGet.CommandLine -Version 4.4.1

相关文章:

  • 2021-07-29
  • 2022-12-23
  • 2022-12-23
  • 2021-05-26
  • 2021-08-11
  • 2022-12-23
  • 2022-02-28
  • 2021-09-16
猜你喜欢
  • 2021-11-21
  • 2021-10-05
  • 2022-12-23
  • 2021-07-21
  • 2022-02-12
相关资源
相似解决方案