C # NuGet Package management commands

Source: Internet
Author: User

The NuGet package Manager Console is built into Visual Studio in Windows 2012 and later versions. (not included in Visual Studio for MAC or Visual Studio Code.) )

Console, you can use the NuGet PowerShell command to find, install, uninstall, and update NuGet packages. using the console is required if the Package Manager UI does not provide instructions on how to perform the operation. to use the nuget.exe command in the console, see Using the CLI Nuget.exe in the console.

For example, to find and install a package, you can use three simple steps:

    1. In Visual Studio, open the project/solution and open the console using Tools > NuGet Package Manager > Package Manager console commands.

    2. Find the package you want to install. If you already know this, skip to step 3.

      PS Copy
      # Find packages containing the keyword "elmah"Find-Package elmah
    3. To run the Install command:

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

Important

All the actions available in the console can also be associated with the NuGet CLI. However, the console commands run in the context of Visual Studio and the saved project/solution, and you typically complete multiple of their equivalent CLI commands. For example, installing a package through the console adds a reference to the project and the CLI command does not run. to do this, developers who typically work in Visual Studio prefer to use the CLI to the console.

Tips

Many console actions depend on opening the solution in Visual Studio with a known path name. If you have an unsaved solution or no solution, you can see this error, "The solution is not open or saved." Make sure that you have opened and saved the solution. " This means that the console cannot determine the folder for the solution. Save the unsaved solution, or create and save the solution, and correct the error if you have not yet opened it.

opening consoles and console controls
    1. open Visual Studio using the console tool > NuGet Package Manager > Package Manager console commands.   In the console, you can arrange and position your favorite Visual Studio Windows (see Customizing window layouts in Visual Studio).

    2. By default, the console command runs at the top of the window for a specific package source and the controls in the project:

    3. Select a different package source and/or project to change subsequent commands for these default settings.  -source and -projectname options.

    4. to manage the package source, select the gear icon.   This is a pointer to the Package Manager UI page in the shortcut tools > Options > NuGet Package Manager > package Source dialog box.  

    5. The rightmost button interrupts long-running commands.   For example, run get-package-listavailable-pagesize to list the default sources (such as nuget.org), The first 500 packages that may take several minutes to run.

packages that are installedPS copy
# Add the Elmah package to the default project as specified in the console‘s project selectorInstall-Package Elmah# Add the Elmah package to a project named UtilitiesLib that is not the defaultInstall-Package Elmah -ProjectName UtilitiesLib

See installation package.

The installed package performs the following actions:

  • The applicable license terms are displayed in the console window with the implied agreement. If you do not agree to these terms, you should uninstall the package immediately.
  • Add a reference to the project in any reference format that you are using. The reference will then appear in Solution Explorer and the applicable reference format file. note, however, that with packagereference, you need to save the project before you can see the changes directly in the project file.
  • Cache Package:
    • Packagereference: The cache package %USERPROFILE%\.nuget\packages and lock file are project.assets.json updated.
    • packages.config: Create a packages folder in the package file into a subfolder of the solution root and replica. package.config Update the file.
  • Update app.config and/or web.config if the package uses source and profile conversions.
  • If the project does not already exist, install any dependencies. This may be updated in the process, as described in package version dependency resolution.
  • If available, display the Readme file for the package in the Visual Studio window.

Tips

One of the main advantages of installing a package Install-Package in a command in the console is that it will reference the project, just like using the Package Manager UI. On the contrary, nuget install The CLI command downloads only the package and does not automatically add references.

Uninstalling PackagesPS copy
# Uninstalls the Elmah package from the default projectUninstall-Package Elmah# Uninstalls the Elmah package and all its unused dependenciesUninstall-Package Elmah -RemoveDependencies # Uninstalls the Elmah package even if another package depends on itUninstall-Package Elmah -Force

See uninstalling packages. use the Get package to view all packages that are currently installed in the default project if you need to find identifiers.

The uninstall package performs the following actions:

    • A reference to the package from the project (and any reference formats that are being used). references no longer appear in Solution Explorer. (you may need to rebuild the project to see it removed from the Bin folder.) )
    • Reverses any changes made to app.config or web.config when the package has been installed.
    • If there are no remaining packages that use these dependencies, the dependencies are removed from the previous installation.

Tips

Install-PackageFor example, the command has the benefit of the Uninstall-Package CLI command that is referenced in the project. nuget uninstall

Update PackagePS copy
# Checks if there are newer versions available for any installed packagesGet-Package -updates# Updates a specific package using its identifier, in this case jQueryUpdate-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 solutionUpdate-Package

See Get packages and update packages

Find PackagesPS copy
 # Find packages containing keywords find-package elmahfind-package logging# List packages whose ID begins with Elmahfind-package elmah-startwith# by default, Get-package returns a list of packages; Use-first To show Morefind-package logging-first 100# List all versions of the package with the ID of" jquery "find-package jquery-allversions-ExactMatch 

See Finding packages. in Visual Studio 2013 and earlier versions, use the Get package instead.

Console Availability

The Visual Studio 2017,nuget and NuGet Package Manager will automatically install when you select any. Provide a. NET-related workload; You can also install it separately by examining the options for individual components > Code Tools > NuGet Package Manager in the Visual Studio 2017 Setup program.

Also, if you are missing NuGet Package Manager in Visual Studio 2015 and earlier, check Tools > Extensions and Updates ... and search NuGet Package Manager extensions. If you are unable to use the extension installer in Visual Studio, you can download it directly from the extension https://dist.nuget.org/index.html.

The Package Manager console is not currently available for the Visual Studio for Mac. equivalent commands, however, are these features through the NuGet CLI. Visual Studio for MAC also has some UI for managing NuGet packages. see in your project including NuGet packages.

The Package Manager console does not include Visual Studio code.

Expansion Pack Manager console

Some packages install a new console command. For example, MvcScaffolding create commands such as the Scaffold following, which will generate the ASP. NET MVC controller and view:

setting up the NuGet PowerShell configuration file

PowerShell profiles can provide commonly used commands as long as you use PowerShell. NuGet support typically finds NuGet-specific profiles in the following locations:

Copy
%UserProfile%\Documents\WindowsPowerShell\NuGet_profile.ps1

To find the configuration file, type $profile in the console:

PS copy
$profileC:\Users\<user>\Documents\WindowsPowerShell\NuGet_profile.ps1

For more details, see Windows PowerShell configuration files.

using the Nuget.exe CLI in the console

To make the nuget.exe CLI available in the Package Manager console, install the Nuget.commandline package from the console:

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

C # NuGet Package management commands

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.