How to use the command dotnet add package (with examples)
The dotnet add package
command allows you to add or update a .NET package reference in a project file. It is a useful command for managing dependencies in a .NET project.
Use case 1: Add a package to the project in the current directory
Code:
dotnet add package package
Motivation: This use case is helpful when you want to add a package to the project that is located in the current directory. It eliminates the need to specify the path to the project file.
Explanation:
dotnet add package
: This is the command used to add a package reference to a project.package
: This is the name of the package you want to add.
Example output:
info : Adding PackageReference for package 'package' to project 'ProjectName.csproj'.
info : Restoring packages for 'ProjectName.csproj'...
info : Package 'package' is compatible with all the specified frameworks in project 'ProjectName.csproj'.
info : Successfully added package 'package' to project 'ProjectName.csproj'.
Use case 2: Add a package to a specific project
Code:
dotnet add path/to/file.csproj package package
Motivation: In some cases, you may have multiple projects in a single directory and want to add a package to a specific project. This use case allows you to specify the path to the project file where you want to add the package.
Explanation:
dotnet add
: This is the command used to add a package reference to a project.path/to/file.csproj
: This is the path to the project file where you want to add the package.package
: This is the name of the package you want to add.
Example output:
info : Adding PackageReference for package 'package' to project 'path/to/file.csproj'.
info : Restoring packages for 'path/to/file.csproj'...
info : Package 'package' is compatible with all the specified frameworks in project 'path/to/file.csproj'.
info : Successfully added package 'package' to project 'path/to/file.csproj'.
Use case 3: Add a specific version of a package to the project
Code:
dotnet add package package --version 1.0.0
Motivation: Sometimes you may need to use a specific version of a package in your project. This use case allows you to specify the version of the package you want to add.
Explanation:
dotnet add package
: This is the command used to add a package reference to a project.package
: This is the name of the package you want to add.--version 1.0.0
: This argument specifies the version of the package you want to add.
Example output:
info : Adding PackageReference for package 'package' version '1.0.0' to project 'ProjectName.csproj'.
info : Restoring packages for 'ProjectName.csproj'...
info : Package 'package' is compatible with all the specified frameworks in project 'ProjectName.csproj'.
info : Successfully added package 'package' version '1.0.0' to project 'ProjectName.csproj'.
Use case 4: Add a package using a specific NuGet source
Code:
dotnet add package package --source https://api.nuget.org/v3/index.json
Motivation: In some cases, you may need to use a custom NuGet source to install a package. This use case allows you to specify the NuGet source from which you want to install the package.
Explanation:
dotnet add package
: This is the command used to add a package reference to a project.package
: This is the name of the package you want to add.--source https://api.nuget.org/v3/index.json
: This argument specifies the NuGet source from which you want to install the package.
Example output:
info : Adding PackageReference for package 'package' to project 'ProjectName.csproj' from source 'https://api.nuget.org/v3/index.json'.
info : Restoring packages for 'ProjectName.csproj'...
info : Package 'package' is compatible with all the specified frameworks in project 'ProjectName.csproj'.
info : Successfully added package 'package' to project 'ProjectName.csproj'.
Use case 5: Add a package only when targeting a specific framework
Code:
dotnet add package package --framework net7.0
Motivation: If your project contains multiple target frameworks, you may want to add a package only for a specific target framework. This use case allows you to specify the framework for which you want to add the package.
Explanation:
dotnet add package
: This is the command used to add a package reference to a project.package
: This is the name of the package you want to add.--framework net7.0
: This argument specifies the target framework for which you want to add the package.
Example output:
info : Adding PackageReference for package 'package' to project 'ProjectName.csproj' with framework 'net7.0'.
info : Restoring packages for 'ProjectName.csproj'...
info : Package 'package' is compatible with framework 'net7.0' in project 'ProjectName.csproj'.
info : Successfully added package 'package' to project 'ProjectName.csproj'.
Use case 6: Add and specify the directory where to restore packages
Code:
dotnet add package package --package-directory path/to/directory
Motivation:
By default, packages are restored to the ~/.nuget/packages
directory. This use case allows you to specify a custom directory where you want to restore packages.
Explanation:
dotnet add package
: This is the command used to add a package reference to a project.package
: This is the name of the package you want to add.--package-directory path/to/directory
: This argument specifies the directory where you want to restore packages.
Example output:
info : Adding PackageReference for package 'package' to project 'ProjectName.csproj'.
info : Restoring packages for 'ProjectName.csproj' to 'path/to/directory'.
info : Package 'package' is compatible with all the specified frameworks in project 'ProjectName.csproj'.
info : Successfully added package 'package' to project 'ProjectName.csproj'.
Conclusion:
The dotnet add package
command is a versatile tool for managing packages in a .NET project. Whether you need to add a package to a specific project, specify a version or framework, or use a custom NuGet source or package directory, this command provides the flexibility needed to handle package dependencies effectively.