
dotnet-runfile
How to use the .NET 10 `dotnet run app.cs` feature. Use this skill when you need to run an existing
提供方 j-r-beckett|开源
dotnet run app.cs
Run C# files directly without .csproj project files. Requires .NET 10 (SpeedReader uses .NET 10, so this should already be installed).
dotnet run hello.cs
Usage Examples
#!/usr/bin/env dotnet run // shebang (unix)
#:sdk Microsoft.NET.Sdk // sdk (default; also .Web, .Worker)
#:package Humanizer@2.14.1 // nuget package (version required, wildcards ok)
#:property LangVersion=preview // msbuild property
#:project ../src/MyLibrary // project reference
Console.WriteLine("Hello, World!");
dotnet run hello.cs # run
dotnet run hello.cs -- arg1 arg2 # run with args
chmod +x hello.cs && ./hello.cs # run via shebang
cat hello.cs | dotnet run - # run from stdin
dotnet publish hello.cs -c Release # publish
dotnet project convert hello.cs # convert to full project w/ csproj
Shared Configuration
Share settings across multiple single-file apps by placing a Directory.Build.props file in the same or parent directory:
<Project>
<PropertyGroup>
<LangVersion>preview</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>
All .cs files in that directory tree inherit these settings.
Cache Location & Clearing
Scripts are cached at ~/.local/share/dotnet/runfile/<script>-<hash>/. The cache includes compiled binaries and all dependencies.
To clear a script's cache (useful when dependencies change but the script doesn't detect it):
dotnet clean hello.cs # clears cache for hello.cs and rebuilds dependencies
This is especially important when:
- A referenced project (
#:project) has been modified - You're debugging stale binary issues
- The script uses the wrong version of a dependency