You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/core/sdk/file-based-apps.md
+58-64Lines changed: 58 additions & 64 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,22 +8,20 @@ ai-usage: ai-assisted
8
8
9
9
**This article applies to:** ✔️ .NET 10 SDK and later versions
10
10
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.
16
12
17
13
Key benefits include:
18
14
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.
23
21
24
22
## Supported directives
25
23
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.
27
25
28
26
### `#:package`
29
27
@@ -34,29 +32,29 @@ Adds a NuGet package reference to your application.
34
32
#:package Serilog version="3.1.1"
35
33
```
36
34
37
-
### `#:property`
35
+
### `#:project`
38
36
39
-
Sets an MSBuild property value.
37
+
References another project file.
40
38
41
39
```csharp
42
-
#:property TargetFramework=net9.0
43
-
#:property PublishAot=false
40
+
#:project ../SharedLibrary/SharedLibrary.csproj
44
41
```
45
42
46
-
### `#:sdk`
43
+
### `#:property`
47
44
48
-
Specifies the SDK to use. Defaults to `Microsoft.NET.Sdk`.
45
+
Sets a MSBuild property value.
49
46
50
47
```csharp
51
-
#:sdk Microsoft.NET.Sdk.Web
48
+
#:property TargetFramework=net10.0
49
+
#:property PublishAot=false
52
50
```
53
51
54
-
### `#:project`
52
+
### `#:sdk`
55
53
56
-
References another project file.
54
+
Specifies the SDK to use. Defaults to `Microsoft.NET.Sdk`.
57
55
58
56
```csharp
59
-
#:project ../SharedLibrary/SharedLibrary.csproj
57
+
#:sdk Microsoft.NET.Sdk.Web
60
58
```
61
59
62
60
## CLI commands
@@ -65,7 +63,7 @@ The .NET CLI provides full support for file-based apps through familiar commands
65
63
66
64
### Run applications
67
65
68
-
Run a file-based app directly:
66
+
Run a file-based app directly using the `dotnet run` command:
69
67
70
68
```dotnetcli
71
69
dotnet run file.cs
@@ -97,29 +95,33 @@ With the shorthand syntax, all arguments go to your application:
97
95
dotnet file.cs arg1 arg2
98
96
```
99
97
100
-
### Restore dependencies
98
+
### Build applications
101
99
102
-
Restore NuGet packages referenced in your file:
100
+
Compile your file-based app using the `dotnet build` command:
103
101
104
102
```dotnetcli
105
-
dotnet restore file.cs
103
+
dotnet build file.cs
106
104
```
107
105
108
-
Restore runs implicitly when you build or run your application.
106
+
The SDK generates a temporary project and builds your application.
109
107
110
-
### Build applications
108
+
### Clean build outputs
111
109
112
-
Compile your file-based app:
110
+
Remove build artifacts using the `dotnet clean` command:
113
111
114
112
```dotnetcli
115
-
dotnet build file.cs
113
+
dotnet clean file.cs
116
114
```
117
115
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
+
```
119
121
120
122
### Publish applications
121
123
122
-
Create a deployment package:
124
+
Create a deployment package using the `dotnet publish` command:
123
125
124
126
```dotnetcli
125
127
dotnet publish file.cs
@@ -129,7 +131,7 @@ File-based apps enable native AOT publishing by default, producing optimized, se
129
131
130
132
### Package as tool
131
133
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:
133
135
134
136
```dotnetcli
135
137
dotnet pack file.cs
@@ -139,49 +141,41 @@ File-based apps set `PackAsTool=true` by default.
139
141
140
142
### Convert to project
141
143
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:
143
145
144
146
```dotnetcli
145
147
dotnet project convert file.cs
146
148
```
147
149
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.
149
151
150
-
### Clean build outputs
152
+
### Restore dependencies
151
153
152
-
Remove build artifacts:
154
+
Restore NuGet packages referenced in your file using the `dotnet restore` command:
153
155
154
156
```dotnetcli
155
-
dotnet clean file.cs
157
+
dotnet restore file.cs
156
158
```
157
159
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.
163
161
164
162
## Default included items
165
163
166
164
File-based apps automatically include specific file types for compilation and packaging.
167
165
168
-
### Standard includes
169
-
170
166
By default, the following items are included:
171
167
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.
176
170
177
-
Different SDKs include additional file types:
171
+
Different SDKs include other file types:
178
172
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.
181
175
182
176
## Native AOT publishing
183
177
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.
185
179
186
180
If you need to disable native AOT, use the following setting:
187
181
@@ -232,7 +226,7 @@ Run directly:
232
226
233
227
## Implicit build files
234
228
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.
236
230
237
231
### `Directory.Build.props`
238
232
@@ -254,8 +248,6 @@ Configures NuGet package sources and settings. File-based apps use these configu
254
248
255
249
Specifies the .NET SDK version to use. File-based apps respect this version selection.
256
250
257
-
Be mindful of these files when organizing your file-based apps. They can unexpectedly affect build behavior.
258
-
259
251
## Build caching
260
252
261
253
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:
269
261
- SDK version
270
262
- Implicit build files
271
263
272
-
### Cache impacts
273
-
274
264
Caching improves build performance but can cause confusion:
275
265
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.
278
268
279
269
### Workarounds
280
270
281
-
Force a clean build to bypass cache:
271
+
- Run a full build using the `--no-cache` flag:
282
272
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:
287
278
288
-
Or delete the `obj` and `bin` directories manually.
0 commit comments