dotnet commands
The dotnet
command-line interface (CLI) is a powerful tool for managing .NET applications and projects. It allows you to perform various tasks such as creating projects, restoring dependencies, building applications, running tests, and more. Here's an overview of commonly used dotnet
commands with examples:
1. Create a New Project
Use dotnet new
to create a new .NET project.
dotnet new console -n MyConsoleApp
- Example: Creates a new console application named
MyConsoleApp
.
2. Restore Dependencies
Use dotnet restore
to restore dependencies specified in the project file (*.csproj
).
dotnet restore
- Example: Restores NuGet packages for the project.
3. Build the Project
Use dotnet build
to build a .NET project.
dotnet build
- Example: Compiles the project and generates binaries.
4. Run the Project
Use dotnet run
to build and run the project.
dotnet run
- Example: Builds and executes the project.
5. Add NuGet Packages
Use dotnet add package
to add NuGet packages to the project.
dotnet add package Newtonsoft.Json
- Example: Adds the
Newtonsoft.Json
package to the project.
6. List Installed SDKs
Use dotnet --list-sdks
to list installed .NET SDKs.
dotnet --list-sdks
- Example: Displays the installed .NET SDK versions.
7. List Installed Runtimes
Use dotnet --list-runtimes
to list installed .NET runtimes.
dotnet --list-runtimes
- Example: Displays the installed .NET runtime versions.
8. Publish the Project
Use dotnet publish
to publish the project for deployment.
dotnet publish -c Release -o ./publish
- Example: Publishes the project in Release configuration to the
./publish
directory.
9. Clean the Project
Use dotnet clean
to clean the build output of the project.
dotnet clean
- Example: Removes all build artifacts and binaries.
10. Run Tests
Use dotnet test
to run tests in the project.
dotnet test
- Example: Executes all unit tests in the project.
11. Manage EF Core Migrations
Use dotnet ef
to manage Entity Framework Core migrations.
dotnet ef migrations add InitialCreate
dotnet ef database update
- Example: Creates a new migration named
InitialCreate
and updates the database schema.
12. Manage Global Tools
Use dotnet tool install
to install global tools.
dotnet tool install -g dotnet-ef
- Example: Installs the
dotnet-ef
tool globally.
13. Generate Documentation
Use dotnet doc
to generate documentation for the project.
dotnet doc
- Example: Creates XML documentation files for the project.
14. Advanced Commands
dotnet pack
: Creates a NuGet package.dotnet nuget
: Manages NuGet package sources.dotnet new
: Lists available project templates (dotnet new --list
).