Introduction to the .NET CLI
The .NET Command-Line Interface (CLI) is a cross-platform toolchain that enables you to build, test, run, and publish .NET applications. It's an essential tool for developers working with the .NET ecosystem, providing a consistent and powerful way to manage your projects from the terminal.
Whether you're creating web applications, desktop apps, services, or libraries, the .NET CLI simplifies development workflows. It integrates seamlessly with your favorite IDEs and text editors.
Getting Started with the .NET CLI
To use the .NET CLI, you first need to have the .NET SDK installed. The SDK includes the CLI tools and the runtime.
You can download the latest .NET SDK from the official .NET download page.
Once installed, you can verify your installation by opening a terminal or command prompt and running:
dotnet --version
This command will display the installed .NET SDK version.
Core .NET CLI Commands
The .NET CLI offers a wide range of commands to manage your development projects. Here are some of the most frequently used commands:
dotnet new
Creates a new .NET project or solution based on a template. Examples include console applications, web applications, class libraries, and more.
dotnet new console -o MyConsoleApp
This command creates a new console application in a directory named 'MyConsoleApp'.
dotnet add
Adds references to project files. This is commonly used to add package references.
dotnet add package Newtonsoft.Json
Adds the Newtonsoft.Json NuGet package to the current project.
dotnet build
Compiles the .NET project.
dotnet build
Builds the project in the current directory.
dotnet run
Compiles and runs the .NET project. This is useful for testing applications during development.
dotnet run
Builds and executes the project in the current directory.
dotnet publish
Publishes the .NET application for deployment. This creates a deployable artifact.
dotnet publish -c Release -o ./publish
Publishes the project in Release configuration to the 'publish' folder.
dotnet test
Runs unit tests in a project.
dotnet test
Discovers and runs tests in the current project and its dependencies.
dotnet clean
Cleans the project output files.
dotnet clean
Removes the bin and obj directories for the project.
Project Management Commands
dotnet restore
Restores the .NET project dependencies (NuGet packages).
dotnet restore
Restores dependencies for the project in the current directory.
dotnet pack
Creates a NuGet package for the project.
dotnet pack
Generates a NuGet package file (.nupkg) for the project.
dotnet list
Lists project references, package references, or project SDKs.
dotnet list package
Lists all package references in the current project.
SDK and Runtime Management
dotnet install
Installs a .NET SDK version. (Note: This command is deprecated in favor of direct downloads, but may be present in older versions or specific scenarios.)
Consider visiting the official download page for the most up-to-date SDKs.
dotnet list sdks
Lists installed .NET SDKs.
dotnet list sdks
dotnet list runtimes
Lists installed .NET runtimes.
dotnet list runtimes
dotnet tool
Manages .NET tools (global, local, and path tools).
dotnet tool install --global --version 6.0.100 Microsoft.CodeAnalysis.NetAnalyzers
Installs the Microsoft.CodeAnalysis.NetAnalyzers tool globally.
Further Exploration
The .NET CLI has many more commands and options. You can explore them by using the --help
flag with any command.
dotnet --help
dotnet new --help
For comprehensive documentation, including detailed command references, guides, and tutorials, please refer to the official Microsoft .NET documentation website.