Skip to content

Commit 503a525

Browse files
committed
edit pass
1 parent fcaa9ff commit 503a525

File tree

1 file changed

+58
-64
lines changed

1 file changed

+58
-64
lines changed

docs/core/sdk/file-based-apps.md

Lines changed: 58 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,20 @@ ai-usage: ai-assisted
88

99
**This article applies to:** ✔️ .NET 10 SDK and later versions
1010

11-
File-based apps let you build, run, and publish .NET applications from a single C# file without creating a traditional project file. This approach simplifies development for scripts, utilities, and small applications. The .NET SDK automatically generates the necessary project configuration based on directives in your source file.
12-
13-
## Overview
14-
15-
File-based apps offer a lightweight alternative to traditional .NET projects. Instead of maintaining separate `.csproj` files, you embed configuration directly in your C# source file using special directives. The .NET CLI processes these directives to build and run your application.
11+
File-based apps let you build, run, and publish .NET applications from a single C# file without creating a traditional project file. They offer a lightweight alternative to traditional .NET projects. This approach simplifies development for scripts, utilities, and small applications. The .NET SDK automatically generates the necessary project configuration based on the directives in your source file.
1612

1713
Key benefits include:
1814

19-
- Reduced boilerplate for simple applications
20-
- Self-contained source files with embedded configuration
21-
- Native AOT publishing enabled by default
22-
- Automatic packaging as .NET tools
15+
- Reduced boilerplate for simple applications.
16+
- Self-contained source files with embedded configuration.
17+
- Native AOT publishing enabled by default.
18+
- Automatic packaging as .NET tools.
19+
20+
In this article, learn more about how to use file-based apps successfully.
2321

2422
## Supported directives
2523

26-
File-based apps use directives prefixed with `#:` to configure the build. Place these directives at the top of your C# file.
24+
File-based apps use directives prefixed with `#:` to configure the build and run your application. Supported directives include: `#:package`, `#:project`, `#:property`, and `#:sdk`. Place these directives at the top of your C# file.
2725

2826
### `#:package`
2927

@@ -34,29 +32,29 @@ Adds a NuGet package reference to your application.
3432
#:package Serilog version="3.1.1"
3533
```
3634

37-
### `#:property`
35+
### `#:project`
3836

39-
Sets an MSBuild property value.
37+
References another project file.
4038

4139
```csharp
42-
#:property TargetFramework=net9.0
43-
#:property PublishAot=false
40+
#:project ../SharedLibrary/SharedLibrary.csproj
4441
```
4542

46-
### `#:sdk`
43+
### `#:property`
4744

48-
Specifies the SDK to use. Defaults to `Microsoft.NET.Sdk`.
45+
Sets a MSBuild property value.
4946

5047
```csharp
51-
#:sdk Microsoft.NET.Sdk.Web
48+
#:property TargetFramework=net10.0
49+
#:property PublishAot=false
5250
```
5351

54-
### `#:project`
52+
### `#:sdk`
5553

56-
References another project file.
54+
Specifies the SDK to use. Defaults to `Microsoft.NET.Sdk`.
5755

5856
```csharp
59-
#:project ../SharedLibrary/SharedLibrary.csproj
57+
#:sdk Microsoft.NET.Sdk.Web
6058
```
6159

6260
## CLI commands
@@ -65,7 +63,7 @@ The .NET CLI provides full support for file-based apps through familiar commands
6563

6664
### Run applications
6765

68-
Run a file-based app directly:
66+
Run a file-based app directly using the `dotnet run` command:
6967

7068
```dotnetcli
7169
dotnet run file.cs
@@ -97,29 +95,33 @@ With the shorthand syntax, all arguments go to your application:
9795
dotnet file.cs arg1 arg2
9896
```
9997

100-
### Restore dependencies
98+
### Build applications
10199

102-
Restore NuGet packages referenced in your file:
100+
Compile your file-based app using the `dotnet build` command:
103101

104102
```dotnetcli
105-
dotnet restore file.cs
103+
dotnet build file.cs
106104
```
107105

108-
Restore runs implicitly when you build or run your application.
106+
The SDK generates a temporary project and builds your application.
109107

110-
### Build applications
108+
### Clean build outputs
111109

112-
Compile your file-based app:
110+
Remove build artifacts using the `dotnet clean` command:
113111

114112
```dotnetcli
115-
dotnet build file.cs
113+
dotnet clean file.cs
116114
```
117115

118-
The SDK generates a temporary project and builds your application.
116+
Clean all file-based apps in a directory:
117+
118+
```dotnetcli
119+
dotnet clean file-based-apps
120+
```
119121

120122
### Publish applications
121123

122-
Create a deployment package:
124+
Create a deployment package using the `dotnet publish` command:
123125

124126
```dotnetcli
125127
dotnet publish file.cs
@@ -129,7 +131,7 @@ File-based apps enable native AOT publishing by default, producing optimized, se
129131

130132
### Package as tool
131133

132-
Package your file-based app as a .NET tool:
134+
Package your file-based app as a .NET tool using the `dotnet pack` command:
133135

134136
```dotnetcli
135137
dotnet pack file.cs
@@ -139,49 +141,41 @@ File-based apps set `PackAsTool=true` by default.
139141

140142
### Convert to project
141143

142-
Convert your file-based app to a traditional project:
144+
Convert your file-based app to a traditional project using the `dotnet project convert` command:
143145

144146
```dotnetcli
145147
dotnet project convert file.cs
146148
```
147149

148-
This command creates a `.csproj` file with equivalent configuration.
150+
This command creates a `.csproj` file with equivalent SDK and properties. All `#` directives are removed from the `.cs` files and turned into elements in the corresponding `.csproj` files.
149151

150-
### Clean build outputs
152+
### Restore dependencies
151153

152-
Remove build artifacts:
154+
Restore NuGet packages referenced in your file using the `dotnet restore` command:
153155

154156
```dotnetcli
155-
dotnet clean file.cs
157+
dotnet restore file.cs
156158
```
157159

158-
Clean all file-based apps in a directory:
159-
160-
```dotnetcli
161-
dotnet clean file-based-apps
162-
```
160+
Restore runs implicitly when you build or run your application.
163161

164162
## Default included items
165163

166164
File-based apps automatically include specific file types for compilation and packaging.
167165

168-
### Standard includes
169-
170166
By default, the following items are included:
171167

172-
- The single C# file itself
173-
- ResX resource files in the same directory
174-
175-
### SDK-specific includes
168+
- The single C# file itself.
169+
- ResX resource files in the same directory.
176170

177-
Different SDKs include additional file types:
171+
Different SDKs include other file types:
178172

179-
- `Microsoft.NET.Sdk.Web` includes `*.json` configuration files
180-
- Other specialized SDKs might include additional patterns
173+
- `Microsoft.NET.Sdk.Web` includes `*.json` configuration files.
174+
- Other specialized SDKs might include other patterns.
181175

182176
## Native AOT publishing
183177

184-
File-based apps enable native ahead-of-time (AOT) compilation by default. This produces optimized, self-contained executables with faster startup and smaller memory footprint.
178+
File-based apps enable native ahead-of-time (AOT) compilation by default. This feature produces optimized, self-contained executables with faster startup, and a smaller memory footprint.
185179

186180
If you need to disable native AOT, use the following setting:
187181

@@ -232,7 +226,7 @@ Run directly:
232226

233227
## Implicit build files
234228

235-
File-based apps respect MSBuild and NuGet configuration files in the same directory or parent directories. These files affect how the SDK builds your application.
229+
File-based apps respect MSBuild and NuGet configuration files in the same directory or parent directories. These files affect how the SDK builds your application. Be mindful of these files when organizing your file-based apps.
236230

237231
### `Directory.Build.props`
238232

@@ -254,8 +248,6 @@ Configures NuGet package sources and settings. File-based apps use these configu
254248

255249
Specifies the .NET SDK version to use. File-based apps respect this version selection.
256250

257-
Be mindful of these files when organizing your file-based apps. They can unexpectedly affect build behavior.
258-
259251
## Build caching
260252

261253
The .NET SDK caches build outputs to improve performance on subsequent builds. File-based apps participate in this caching system.
@@ -269,23 +261,25 @@ The SDK caches build outputs based on:
269261
- SDK version
270262
- Implicit build files
271263

272-
### Cache impacts
273-
274264
Caching improves build performance but can cause confusion:
275265

276-
- Changes to implicit build files might not trigger rebuilds
277-
- Moving files to different directories might not invalidate cache
266+
- Changes to implicit build files might not trigger rebuilds.
267+
- Moving files to different directories might not invalidate cache.
278268

279269
### Workarounds
280270

281-
Force a clean build to bypass cache:
271+
- Run a full build using the `--no-cache` flag:
282272

283-
```dotnetcli
284-
dotnet clean file.cs
285-
dotnet build file.cs
286-
```
273+
```dotnetcli
274+
dotnet build file.cs --no-cache
275+
```
276+
277+
- Force a clean build to bypass cache:
287278

288-
Or delete the `obj` and `bin` directories manually.
279+
```dotnetcli
280+
dotnet clean file.cs
281+
dotnet build file.cs
282+
```
289283

290284
## Folder layout recommendations
291285

0 commit comments

Comments
 (0)