Welcome to dotnet-exec! This guide will help you get started with executing C# scripts and code without the need for a full project setup.
dotnet-exec is a command-line tool that allows you to execute C# programs without creating a project file. It supports:
- Raw C# code execution: Run code directly from the command line
- Script file execution: Execute .cs files locally or from URLs
- Custom entry points: Use methods other than
Mainas entry points - REPL mode: Interactive C# execution environment
- Rich reference support: NuGet packages, local DLLs, framework references
- Testing capabilities: Built-in xUnit test execution
- Configuration profiles: Save and reuse common configurations
- Command aliases: Create shortcuts for frequently used commands
Install the latest stable version:
dotnet tool install -g dotnet-executeInstall the latest preview version:
dotnet tool install -g dotnet-execute --prereleaseUpdate to the latest version:
dotnet tool update -g dotnet-executeIf installation fails, try:
# Add NuGet source explicitly
dotnet tool install -g dotnet-execute --add-source https://api.nuget.org/v3/index.json
# Clear cache and retry
dotnet nuget locals all --clear
dotnet tool install -g dotnet-execute
# Ignore failed sources
dotnet tool install -g dotnet-execute --ignore-failed-sourcesYou can also use dotnet-exec with Docker/Podman without installing .NET SDK:
# Docker
docker run --rm weihanli/dotnet-exec:latest "1+1"
docker run --rm weihanli/dotnet-exec:latest "Guid.NewGuid()"
# Podman
podman run --rm weihanli/dotnet-exec:latest "DateTime.Now"For the full image tag list, see https://hub.docker.com/r/weihanli/dotnet-exec/tags
# Basic arithmetic
dotnet-exec "1 + 1"
# Generate a GUID
dotnet-exec "Guid.NewGuid()"
# Get current time
dotnet-exec "DateTime.Now"
# String manipulation
dotnet-exec "\"Hello World\".ToUpper()"# Print to console
dotnet-exec 'Console.WriteLine("Hello, dotnet-exec!");'
# Loop and calculations
dotnet-exec 'for(int i = 1; i <= 5; i++) Console.WriteLine($"Square of {i} is {i*i}");'
# Work with collections
dotnet-exec 'var numbers = new[] {1,2,3,4,5}; Console.WriteLine($"Sum: {numbers.Sum()}");'Create a file hello.cs:
Console.WriteLine("Hello from script file!");
Console.WriteLine($"Current time: {DateTime.Now}");Execute it:
dotnet-exec hello.cs# Execute from GitHub
dotnet-exec https://raw.githubusercontent.com/user/repo/main/script.cs
# Execute from any URL
dotnet-exec https://example.com/scripts/utility.csdotnet-exec looks for these entry methods in order:
- Custom entry point specified with
--entry - Default entry methods:
MainTest,Execute,Run - Standard
Mainmethod
Create a script with custom entry point:
// custom-entry.cs
public class MyScript
{
public static void MainTest()
{
Console.WriteLine("Custom entry point executed!");
}
public static void Execute()
{
Console.WriteLine("Alternative entry point");
}
}Execute with specific entry point:
dotnet-exec custom-entry.cs --entry MainTestConfigure fallback entry methods with profile:
dotnet-exec profile set my-entries --default-entry CustomMain --default-entry Execute --default-entry Run
dotnet-exec script.cs --profile=my-entriesStart interactive mode by running dotnet-exec without arguments:
dotnet-execIn REPL mode, you can:
// Execute expressions
> 1 + 1
2
// Reference NuGet packages
> #r "nuget:Newtonsoft.Json"
> using Newtonsoft.Json;
// Use code completion
> Console.? // Press ? for IntelliSense
// Multi-line expressions
> var data = new {
Name = "Test",
Value = 42
};
> data
{ Name = Test, Value = 42 }# Start REPL with web references
dotnet-exec --web
# Start with custom profile
dotnet-exec --profile myprofile
# Start with additional references
dotnet-exec -r 'nuget:Serilog' -u 'Serilog'# Latest stable version
dotnet-exec 'JsonConvert.SerializeObject(new {name="test"})' \
-r 'nuget:Newtonsoft.Json' \
-u 'Newtonsoft.Json'
# Specific version
dotnet-exec 'JsonConvert.SerializeObject(new {name="test"})' \
-r 'nuget:Newtonsoft.Json,13.0.3' \
-u 'Newtonsoft.Json'# Reference local DLL
dotnet-exec MyScript.cs -r './libs/MyLibrary.dll'
# Reference all DLLs in folder
dotnet-exec MyScript.cs -r 'folder:./libs'# Web framework (ASP.NET Core)
dotnet-exec 'WebApplication.Create().Run();' --web
# Explicit framework reference
dotnet-exec 'WebApplication.Create().Run();' -r 'framework:web'# Static using
dotnet-exec 'WriteLine("Hello World");' -u 'static System.Console'
# Using alias
dotnet-exec 'Json.SerializeObject(data)' \
-u 'Json = Newtonsoft.Json.JsonConvert' \
-r 'nuget:Newtonsoft.Json'
# Remove default using
dotnet-exec 'System.Console.WriteLine("Hello");' -u '-System'The default command executes C# scripts and code:
# Execute script file
dotnet-exec script.cs
# Execute raw code
dotnet-exec 'Console.WriteLine("Hello");'
# Start REPL
dotnet-execManage configuration profiles:
# List profiles
dotnet-exec profile ls
# Create profile
dotnet-exec profile set myprofile -r 'nuget:Serilog' -u 'Serilog'
# Use profile
dotnet-exec script.cs --profile myprofile
# Get profile details
dotnet-exec profile get myprofile
# Remove profile
dotnet-exec profile rm myprofileManage command aliases:
# List aliases
dotnet-exec alias ls
# Create alias
dotnet-exec alias set guid "Guid.NewGuid()"
# Use alias
dotnet-exec guid
# Remove alias
dotnet-exec alias unset guidExecute xUnit tests:
# Run test file
dotnet-exec test MyTests.cs
# Run multiple test files
dotnet-exec test Test1.cs Test2.cs Test3.cs
# Run tests with additional references
dotnet-exec test MyTests.cs -r 'nuget:Moq' -u 'Moq'# Enable debug output
dotnet-exec script.cs --debug
# Dry run (compile but don't execute)
dotnet-exec script.cs --dry-run
# Use preview language features
dotnet-exec script.cs --preview# Wide references (includes common packages)
dotnet-exec script.cs --wide
# Disable wide references
dotnet-exec script.cs --wide false
# Web references
dotnet-exec script.cs --web# Set environment variables
dotnet-exec script.cs --env 'VAR1=value1' --env 'VAR2=value2'
# Set execution timeout
dotnet-exec script.cs --timeout 300
# Use specific .NET framework
dotnet-exec script.cs --framework net10.0Now that you understand the basics, explore these guides for more advanced usage:
- Advanced Usage Guide: Complex scenarios, multiple scripts, and advanced options
- References Guide: Comprehensive reference management
- Profiles and Aliases: Configuration management and shortcuts
- Testing Guide: xUnit integration and testing workflows
- Examples and Use Cases: Real-world examples across different domains
- Troubleshooting: Common issues and solutions
# General help
dotnet-exec --help
# Command-specific help
dotnet-exec profile --help
dotnet-exec alias --help
dotnet-exec test --help
# System information
dotnet-exec --infoFor additional support, visit the GitHub repository or check the troubleshooting guide.