dotnet-file-based-apps
Original:🇺🇸 English
Translated
Builds .NET 10 file-based C# apps. Directives, CLI commands, csproj migration.
2installs
Added on
NPX Install
npx skill4agent add novotnyllc/dotnet-artisan dotnet-file-based-appsTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →dotnet-file-based-apps
.NET 10 SDK file-based apps let you build, run, and publish C# applications from a single file without creating a project file. The SDK auto-generates project configuration from directives embedded in the source file. This feature targets scripts, utilities, and small applications where traditional project scaffolding is unnecessary.
.cs.csproj#:This is NOT file I/O. For FileStream, RandomAccess, FileSystemWatcher, and path handling, see [skill:dotnet-file-io].
Prerequisites: Requires .NET 10 SDK or later. Run [skill:dotnet-version-detection] to confirm SDK version.
Cross-references: [skill:dotnet-version-detection] for SDK version gating, [skill:dotnet-project-analysis] for project-based analysis (file-based apps have no ), [skill:dotnet-scaffold-project] for csproj-based project scaffolding.
.csprojScope
- #: directives (package, sdk, property, project)
- CLI commands for file-based apps (dotnet run, dotnet publish)
- Migration from file-based to .csproj project format
Out of scope
- File I/O (FileStream, RandomAccess, paths) -- see [skill:dotnet-file-io]
- Project-based .csproj scaffolding -- see [skill:dotnet-scaffold-project]
- Solution structure analysis -- see [skill:dotnet-project-analysis]
Directives Overview
File-based apps use directives to configure the build. Directives are SDK-level instructions, not C# syntax. They must appear at the top of the file, before any C# code.
#:.csFour directive types are supported:
| Directive | Purpose | Example |
|---|---|---|
| Add a NuGet package reference | |
| Set the SDK (default: | |
| Set an MSBuild property | |
| Reference another project file | |
#:package
Directive
#:packageAdds a NuGet package reference. Specify the package name, optionally followed by .
@versioncsharp
#:package Newtonsoft.Json
#:package Serilog@3.1.1
#:package Spectre.Console@*Version behavior:
- -- pins to a specific version
@version - -- uses the latest stable version (NuGet floating version)
@* - No version -- only works when Central Package Management (CPM) is configured with a file; otherwise, specify a version explicitly or use
Directory.Packages.props@*
#:sdk
Directive
#:sdkSpecifies which SDK to use. Defaults to if omitted.
Microsoft.NET.Sdkcsharp
#:sdk Microsoft.NET.Sdk.Webcsharp
#:sdk Aspire.AppHost.Sdk@9.2.0Use this directive to access SDK-specific features. For example, enables ASP.NET Core features and automatically includes configuration files in the build.
Microsoft.NET.Sdk.Web*.json#:property
Directive
#:propertySets an MSBuild property value. Use this to customize build behavior.
csharp
#:property TargetFramework=net10.0
#:property PublishAot=falseConditional Property Values
Property directives support MSBuild property functions and expressions for conditional configuration.
Environment variables with defaults:
csharp
#:property LogLevel=$([MSBuild]::ValueOrDefault('$(LOG_LEVEL)', 'Information'))Conditional expressions:
csharp
#:property EnableLogging=$([System.Convert]::ToBoolean($([MSBuild]::ValueOrDefault('$(ENABLE_LOGGING)', 'true'))))#:project
Directive
#:projectReferences another project file or directory containing a project file. Use this to share code between a file-based app and a traditional project.
csharp
#:project ../SharedLibrary/SharedLibrary.csprojThe referenced project is built and linked as a project reference, just like in a .
<ProjectReference>.csprojCLI Commands
The .NET CLI supports file-based apps through familiar commands.
Run
bash
# Preferred: pass file directly
dotnet run app.cs
# Explicit --file option
dotnet run --file app.cs
# Shorthand (no 'run' subcommand)
dotnet app.cs
# Pass arguments after --
dotnet run app.cs -- arg1 arg2When a exists in the current directory, (without ) runs the project and passes as an argument to preserve backward compatibility. Use to force file-based execution.
.csprojdotnet run app.cs--fileapp.csdotnet run --file app.csPipe from stdin
bash
echo 'Console.WriteLine("hello");' | dotnet run -The argument reads C# code from standard input. Useful for quick testing and shell script integration.
-Build
bash
dotnet build app.csBuild output goes to a cached location under the system temp directory by default. Override with or .
--output#:property OutputPath=./outputClean
bash
# Clean build artifacts for a specific file
dotnet clean app.cs
# Clean all file-based app caches in the current directory
dotnet clean file-based-apps
# Clean caches unused for N days (default: 30)
dotnet clean file-based-apps --days 7Publish
bash
dotnet publish app.csFile-based apps enable native AOT by default. The output goes to an directory next to the file. Disable AOT with .
artifacts.cs#:property PublishAot=falsePack as .NET Tool
bash
dotnet pack app.csFile-based apps set by default. Disable with .
PackAsTool=true#:property PackAsTool=falseRestore
bash
dotnet restore app.csRestore runs implicitly on build/run. Pass to or to skip it.
--no-restoredotnet builddotnet runShell Execution (Unix)
Enable direct execution on Unix-like systems with a shebang line.
csharp
#!/usr/bin/env dotnet
#:package Spectre.Console
using Spectre.Console;
AnsiConsole.MarkupLine("[green]Hello, World![/]");bash
chmod +x app.cs
./app.csThe file must use line endings (not ) and must not include a BOM.
LFCRLFLaunch Profiles
File-based apps support launch profiles via a flat file in the same directory as the source file. For , create :
[AppName].run.jsonapp.csapp.run.jsonjson
{
"profiles": {
"https": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}Select a profile with :
--launch-profilebash
dotnet run app.cs --launch-profile httpsIf both and exist, the traditional location takes priority.
app.run.jsonProperties/launchSettings.jsonUser Secrets
File-based apps generate a stable user secrets ID from the file's full path.
bash
dotnet user-secrets set "ApiKey" "your-secret-value" --file app.cs
dotnet user-secrets list --file app.csImplicit Build Files
File-based apps respect MSBuild and NuGet configuration files in the same or parent directories:
- -- inherited MSBuild properties
Directory.Build.props - -- inherited MSBuild targets
Directory.Build.targets - -- Central Package Management versions
Directory.Packages.props - -- NuGet package source configuration
nuget.config - -- SDK version pinning
global.json
Be mindful of these files when placing file-based apps in a repository that also contains traditional projects. Inherited properties may cause unexpected build behavior.
Build Caching
The SDK caches build outputs based on source content, directives, SDK version, and implicit build files. Caching improves repeated performance.
dotnet runKnown caching pitfalls:
- Changes to implicit build files (, etc.) may not trigger rebuilds
Directory.Build.props - Moving files to different directories does not invalidate the cache
- Concurrent execution of the same file-based app can cause build contention errors -- build first with , then run multiple instances with
dotnet build app.csdotnet run app.cs --no-build
Clear the cache with or .
dotnet clean app.csdotnet clean file-based-appsFolder Layout
Do not place file-based apps inside a project's directory tree. The project's implicit build configuration will interfere.
.csproj# Recommended layout
repo/
src/
MyProject/
MyProject.csproj
Program.cs
scripts/ # Separate directory for file-based apps
utility.cs
tool.csMigration: File-Based to Project-Based
When a file-based app outgrows a single file, convert to a traditional project.
Automatic Conversion
bash
dotnet project convert app.csThis creates a new directory named after the app, containing:
- A with equivalent SDK, properties, and package references derived from the
.csprojdirectives#: - A copy of the file with
.csdirectives removed#:
The original file is left untouched.
.csWhen to Convert
Convert to a project-based app when:
- Multiple source files are needed
- Complex MSBuild customization is required beyond what supports
#:property - The app needs with a test framework
dotnet test - The app needs integration with CI/CD workflows that expect a
.csproj - Team members need IDE project support (Solution Explorer, etc.)
Default Behaviors
File-based apps differ from project-based apps in several default settings:
| Setting | File-based default | Project-based default |
|---|---|---|
Native AOT ( | | |
Pack as tool ( | | |
| Build output location | System temp directory | |
| Publish output location | | |
Agent Gotchas
- Do not confuse file-based apps with file I/O -- covers running C# without a project file (
dotnet-file-based-apps). For FileStream, RandomAccess, and path handling, use [skill:dotnet-file-io]..NET 10 SDK feature - Do not use directives after C# code -- all directives must appear at the top of the file, before any C# statements,
#:directives, or namespace declarations. The SDK ignores directives placed later in the file.using - Do not omit package versions without CPM -- without a version only works when Central Package Management is configured via
#:package SomePackage. Without CPM, useDirectory.Packages.propsor#:package SomePackage@1.0.0.#:package SomePackage@* - Do not assume and
dotnet buildwork the same --dotnet testcompiles via a virtual project, butdotnet build app.csdoes not apply to file-based apps. Convert to a project for test framework support.dotnet test - Do not place file-based apps inside a project directory -- the project's implicit build files (
.csproj, etc.) will affect the file-based app, causing unexpected behavior. Use a separate directory.Directory.Build.props - Do not run concurrent instances without pre-building -- parallel execution of the same file-based app causes build output contention. Build first with , then run instances with
dotnet build app.cs.dotnet run app.cs --no-build - Do not forget backward compatibility -- when a exists in the current directory,
.csprojpassesdotnet run app.csas an argument to the project rather than running it as a file-based app. Useapp.csto force file-based execution.dotnet run --file app.cs - Do not use line endings with shebang -- Unix shebang execution requires
CRLFline endings and no BOM. Files withLFwill fail withCRLF./usr/bin/env: 'dotnet\r': No such file or directory