.NET Run Command (with examples)
The dotnet run
command is used to compile and launch a .NET application. It is a convenient way to quickly run your project without explicitly specifying compile or launch commands. In this article, we will explore eight different use cases of the dotnet run
command with code examples.
1. Run the project in the current directory
To run the project in the current directory, you can simply use the following command:
dotnet run
Motivation: This use case allows you to quickly compile and run a .NET project without explicitly specifying the project file or any additional arguments. It is useful when working on a project in a single directory without multiple projects.
Arguments:
- None
Example Output:
If the project is a console application, it will execute the Main
method and display the output in the console.
2. Run a specific project
If you have multiple projects in your solution, you can specify which project to run using the --project
option. Here’s an example:
dotnet run --project path/to/file.csproj
Motivation: This use case is helpful when you have a solution with multiple projects and you want to explicitly run a particular project.
Arguments:
--project
: Specifies the path to the project file you want to run.
Example Output:
The specified project will be compiled and executed, showing the output in the console if it is a console application.
3. Run the project with specific arguments
You can pass specific arguments to your project using the --
option. Here’s an example:
dotnet run -- arg1=foo arg2=bar
Motivation: This use case allows you to provide custom arguments to your project when running it. It can be useful when you want to pass configuration options or command-line parameters to your application.
Arguments:
--
: Separates thedotnet run
arguments from the application arguments.arg1=foo
,arg2=bar
, …: Custom arguments to pass to the application.
Example Output:
The application will receive the specified arguments and can use them for processing or customization.
4. Run the project using a target framework moniker
If your project targets multiple frameworks, you can specify which framework to use when running the project. Here’s an example:
dotnet run --framework net7.0
Motivation: This use case is useful when your project targets multiple frameworks, and you want to explicitly choose a specific framework to run the application with.
Arguments:
--framework
: Specifies the target framework moniker (TFM) to use when running the project.
Example Output:
The project will be compiled and executed using the specified target framework moniker.
5. Specify architecture and OS
Starting from .NET 6, you can specify the architecture and the operating system to run your project on. Here’s an example:
dotnet run --arch x64 --os win
Motivation: This use case is beneficial when you want to target a specific architecture and operating system, ignoring the default platform settings.
Arguments:
--arch
: Specifies the target architecture for the project (x86, x64, arm, arm64).--os
: Specifies the target operating system for the project (win, win7, osx, linux, ios, android).
Example Output:
The project will be compiled and executed on the specified architecture and operating system.
6. Run with specific runtime options
When running a .NET application, you can pass specific runtime options to control how the application behaves. Here’s an example:
dotnet run --framework net7.0 --runtime linux-arm64
Motivation: This use case is useful when you need to run your project with specific runtime options, such as selecting a different runtime or version.
Arguments:
--framework
: Specifies the target framework moniker (TFM) to use when running the project.--runtime
: Specifies the target runtime identifier (RID) to use when running the project.
Example Output:
The project will be compiled and executed using the specified target framework moniker and runtime.
7. Run in a specific configuration
If your project contains multiple configuration profiles, you can specify which configuration to use when running the project. Here’s an example:
dotnet run --configuration Release
Motivation: This use case is beneficial when you want to run your project using a specific configuration, such as release mode for optimal performance.
Arguments:
--configuration
: Specifies the configuration to use when running the project.
Example Output:
The project will be compiled and executed using the specified configuration profile.
8. Run with additional environment variables
You can set additional environment variables when running your project using the --environment
option. Here’s an example:
dotnet run --environment Production
Motivation: This use case allows you to run your project with specific environment variables, which can affect the behavior or configuration of your application.
Arguments:
--environment
: Specifies the environment to use when running the project.
Example Output:
The project will be executed with the specified environment variables, which can be used to customize the behavior or configuration of the application based on the environment.
Conclusion
The dotnet run
command is a versatile tool for quickly compiling and launching .NET applications. By understanding its various use cases and arguments, you can efficiently run your projects with the desired configurations and parameters. Whether you need to run a specific project, pass custom arguments, target a particular framework, or set runtime options, the dotnet run
command has you covered.
Remember to consult the official Microsoft documentation for more detailed information and examples: dotnet run - .NET Core CLI .