C# File-based Apps Skill
This skill covers C# file-based apps on the latest .NET. Use it for single-file .cs execution with dotnet run app.cs or dotnet app.cs, #: directives, publish / pack / convert workflows, launch profiles, user secrets, build caching, and folder layout guidance.
Skill directory
~/.copilot/skills/csharp-file-based-apps/
Quick Reference
- Use
references/file-based-apps.md for the core reference.
- Start from the minimal samples in the
Examples section when you need working code.
Workflow
- First determine whether the user needs a new file-based app, an improvement to an existing utility, a conversion from or to a traditional
.csproj, or troubleshooting.
- Check
references/file-based-apps.md for supported directives, CLI behavior, and constraints. If you need examples, prefer the samples in this SKILL.md and the bundled reference.
- When proposing code or commands, make these points explicit:
- SDK requirements and prerequisites (.NET 10 SDK or later;
#:include requires SDK 10.0.300+ / .NET 11 Preview 3+)
- Which
#: directives are appropriate
- Whether
dotnet run, build, publish, pack, restore, or project convert is the right command
- Whether implicit files such as
Directory.Build.props or global.json might affect the result
- Prefer the smallest single-file example that actually works, and only introduce extra files or conversion steps when there is a clear reason.
- Only state behavior as fact when it is supported by Microsoft Learn. Call out preview-only features and OS-specific differences.
What to help with
- Running, building, and distributing a single
.cs file
- Choosing the right
#: directives
- NuGet package references, project references, and MSBuild properties
- Native AOT defaults and how to disable them
- Packing as a .NET tool
- Converting to a traditional project with
dotnet project convert
- Launch profiles via
app.run.json
dotnet user-secrets ... --file app.cs
- Troubleshooting build cache and folder layout issues
Supported directives
File-based apps place #: directives at the top of the C# file.
| Directive |
Purpose |
Example |
#:package |
Add a NuGet package reference |
#:package Spectre.Console@* |
#:property |
Set an MSBuild property |
#:property PublishAot=false |
#:project |
Reference another project |
#:project ../Shared/Shared.csproj |
#:sdk |
Select the SDK |
#:sdk Microsoft.NET.Sdk.Web |
#:include |
Include extra files |
#:include shared/**/*.cs |
Guidance
- Do not invent unsupported directives. Use only the five directives listed above.
- For
#:package, prefer explicit versions unless the repo uses central package management. Use @* when "latest available" is the goal.
- Included
.cs files in #:include cannot contain top-level statements. Use them for types, methods, namespaces, and related declarations.
#:property can use MSBuild property functions and environment variables, but keep the setup understandable and explain why it is needed.
- For ASP.NET Core or configuration-driven examples, consider
#:sdk Microsoft.NET.Sdk.Web.
CLI patterns
Run
dotnet run app.cs
dotnet app.cs
dotnet run app.cs -- arg1 arg2
- If a
.csproj exists in the current directory, dotnet run app.cs might, for backward compatibility, run that project and pass app.cs as an argument. Use --file when you need unambiguous file-based behavior.
Build / Clean / Restore
dotnet build app.cs
dotnet clean app.cs
dotnet restore app.cs
dotnet clean file-based-apps
Publish / Pack / Convert
dotnet publish app.cs
dotnet pack app.cs
dotnet project convert app.cs
publish enables Native AOT by default.
pack defaults to PackAsTool=true.
- Use
#:property PublishAot=false or #:property PackAsTool=false when you need to override those defaults.
Troubleshooting checklist
1. A project runs when you meant to run the file-based app
- Use
dotnet run --file app.cs.
- Consider moving
app.cs outside the .csproj directory tree.
2. Build caching looks wrong
dotnet clean app.cs
dotnet clean file-based-apps
- If needed, run
dotnet build app.cs and then dotnet run app.cs --no-build
3. Implicit build files are affecting behavior
Directory.Build.props
Directory.Build.targets
Directory.Packages.props
nuget.config
global.json
Any of these in parent directories can affect the file-based app and often explain surprising behavior.
4. The folder layout is working against you
- Avoid placing a utility-style
app.cs inside a .csproj cone.
- Use a separate directory for standalone scripts and utilities.
Output expectations
When responding, prefer this order when it fits the request:
- Shortest correct answer: what to run, add, or change
- Sample code: the smallest single
.cs file that works
- Notes: SDK requirements, preview limitations, cache behavior, or folder layout caveats
If the request involves conversion or architectural choice, explain why a file-based app is a good fit and when a normal .csproj would be a better option.
Examples
Minimal console example
#:package Spectre.Console@*
using Spectre.Console;
AnsiConsole.MarkupLine("[green]Hello from a file-based app[/]");
Run:
dotnet run hello.cs
Disable Native AOT for a quick utility
#:property PublishAot=false
Console.WriteLine("Utility script");
Web app style
#:sdk Microsoft.NET.Sdk.Web
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => Results.Ok(new { message = "hello" }));
app.Run();
References
1---2name: csharp-file-based-apps3description: Use this skill whenever the user is working with C# file-based apps on the latest .NET, including single-file C# programs run with `dotnet run file.cs`, `dotnet file.cs`, or stdin, `#:` directives such as `#:package`, `#:property`, `#:project`, `#:sdk`, and `#:include`, converting utility scripts into file-based apps, packaging them as .NET tools, publishing with Native AOT, or troubleshooting cache, launch profile, and folder layout issues. Trigger even when the user only says "single-file C# app", "projectless C# script", or asks how to run a .cs file directly without creating a .csproj.4license: Proprietary. LICENSE has complete terms.5---67# C# File-based Apps Skill89This skill covers **C# file-based apps** on the latest .NET. Use it for single-file `.cs` execution with `dotnet run app.cs` or `dotnet app.cs`, `#:` directives, publish / pack / convert workflows, launch profiles, user secrets, build caching, and folder layout guidance.1011## Skill directory1213`~/.copilot/skills/csharp-file-based-apps/`1415## Quick Reference1617- Use `references/file-based-apps.md` for the core reference.18- Start from the minimal samples in the `Examples` section when you need working code.1920---2122## Workflow23241. First determine whether the user needs **a new file-based app**, **an improvement to an existing utility**, **a conversion from or to a traditional `.csproj`**, or **troubleshooting**.252. Check `references/file-based-apps.md` for supported directives, CLI behavior, and constraints. If you need examples, prefer the samples in this SKILL.md and the bundled reference.263. When proposing code or commands, make these points explicit:27 - SDK requirements and prerequisites (**.NET 10 SDK or later**; `#:include` requires SDK 10.0.300+ / .NET 11 Preview 3+)28 - Which `#:` directives are appropriate29 - Whether `dotnet run`, `build`, `publish`, `pack`, `restore`, or `project convert` is the right command30 - Whether implicit files such as `Directory.Build.props` or `global.json` might affect the result314. Prefer the **smallest single-file example that actually works**, and only introduce extra files or conversion steps when there is a clear reason.325. Only state behavior as fact when it is supported by Microsoft Learn. Call out preview-only features and OS-specific differences.3334---3536## What to help with3738- Running, building, and distributing a single `.cs` file39- Choosing the right `#:` directives40- NuGet package references, project references, and MSBuild properties41- Native AOT defaults and how to disable them42- Packing as a .NET tool43- Converting to a traditional project with `dotnet project convert`44- Launch profiles via `app.run.json`45- `dotnet user-secrets ... --file app.cs`46- Troubleshooting build cache and folder layout issues4748---4950## Supported directives5152File-based apps place `#:` directives at the top of the C# file.5354| Directive | Purpose | Example |55|-----------|------|----|56| `#:package` | Add a NuGet package reference | `#:package Spectre.Console@*` |57| `#:property` | Set an MSBuild property | `#:property PublishAot=false` |58| `#:project` | Reference another project | `#:project ../Shared/Shared.csproj` |59| `#:sdk` | Select the SDK | `#:sdk Microsoft.NET.Sdk.Web` |60| `#:include` | Include extra files | `#:include shared/**/*.cs` |6162### Guidance6364- Do not invent unsupported directives. Use only the five directives listed above.65- For `#:package`, prefer explicit versions unless the repo uses central package management. Use `@*` when "latest available" is the goal.66- Included `.cs` files in `#:include` cannot contain **top-level statements**. Use them for types, methods, namespaces, and related declarations.67- `#:property` can use MSBuild property functions and environment variables, but keep the setup understandable and explain why it is needed.68- For ASP.NET Core or configuration-driven examples, consider `#:sdk Microsoft.NET.Sdk.Web`.6970---7172## CLI patterns7374### Run7576```bash77dotnet run app.cs78dotnet app.cs79dotnet run app.cs -- arg1 arg280```8182- If a `.csproj` exists in the current directory, `dotnet run app.cs` might, for backward compatibility, **run that project and pass `app.cs` as an argument**. Use `--file` when you need unambiguous file-based behavior.8384### Build / Clean / Restore8586```bash87dotnet build app.cs88dotnet clean app.cs89dotnet restore app.cs90dotnet clean file-based-apps91```9293### Publish / Pack / Convert9495```bash96dotnet publish app.cs97dotnet pack app.cs98dotnet project convert app.cs99```100101- `publish` enables Native AOT by default.102- `pack` defaults to `PackAsTool=true`.103- Use `#:property PublishAot=false` or `#:property PackAsTool=false` when you need to override those defaults.104105---106107## Troubleshooting checklist108109### 1. A project runs when you meant to run the file-based app110111- Use `dotnet run --file app.cs`.112- Consider moving `app.cs` outside the `.csproj` directory tree.113114### 2. Build caching looks wrong115116- `dotnet clean app.cs`117- `dotnet clean file-based-apps`118- If needed, run `dotnet build app.cs` and then `dotnet run app.cs --no-build`119120### 3. Implicit build files are affecting behavior121122- `Directory.Build.props`123- `Directory.Build.targets`124- `Directory.Packages.props`125- `nuget.config`126- `global.json`127128Any of these in parent directories can affect the file-based app and often explain surprising behavior.129130### 4. The folder layout is working against you131132- Avoid placing a utility-style `app.cs` inside a `.csproj` cone.133- Use a separate directory for standalone scripts and utilities.134135---136137## Output expectations138139When responding, prefer this order when it fits the request:1401411. **Shortest correct answer**: what to run, add, or change1422. **Sample code**: the smallest single `.cs` file that works1433. **Notes**: SDK requirements, preview limitations, cache behavior, or folder layout caveats144145If the request involves conversion or architectural choice, explain why a file-based app is a good fit and when a normal `.csproj` would be a better option.146147---148149## Examples150151### Minimal console example152153```csharp154#:package Spectre.Console@*155156using Spectre.Console;157158AnsiConsole.MarkupLine("[green]Hello from a file-based app[/]");159```160161Run:162163```bash164dotnet run hello.cs165```166167### Disable Native AOT for a quick utility168169```csharp170#:property PublishAot=false171172Console.WriteLine("Utility script");173```174175### Web app style176177```csharp178#:sdk Microsoft.NET.Sdk.Web179180var builder = WebApplication.CreateBuilder(args);181var app = builder.Build();182183app.MapGet("/", () => Results.Ok(new { message = "hello" }));184185app.Run();186```187188---189190## References191192- Detailed reference: [references/file-based-apps.md](./references/file-based-apps.md)193- Source domain: https://learn.microsoft.com/en-us/dotnet/core/sdk/file-based-apps