Skip to content

Commit 4506620

Browse files
docs: Add documentation for dotnet project convert command (#50125)
* docs: Add documentation for dotnet project convert command Adds documentation for the dotnet project convert command, which converts file-based programs to project-based programs. The documentation includes: - Command synopsis and description - Conversion process details - All command options (--dry-run, --force, --interactive, --output) - Examples showing conversion from file-based to project-based structure - Translation of #:sdk, #:package, and #:property directives * Address PR feedback * Address PR feedback * Remove empty line in dotnet-project-convert.md * docs: Add dotnet project convert to navigation and index Adds the dotnet project convert command to the CLI index and table of contents. * Address PR feedback --------- Co-authored-by: Meaghan Osagie (Lewis) <[email protected]>
1 parent 9c600fb commit 4506620

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
---
2+
title: dotnet project convert command
3+
description: The 'dotnet project convert' command converts a file-based program to a project-based program.
4+
ms.date: 10/25/2025
5+
ai-usage: ai-assisted
6+
---
7+
# dotnet project convert
8+
9+
**This article applies to:** ✔️ .NET 10 SDK and later versions
10+
11+
## Name
12+
13+
`dotnet project convert` - Converts a file-based program to a project-based program.
14+
15+
## Synopsis
16+
17+
```dotnetcli
18+
dotnet project convert <FILE> [--dry-run] [--force] [--interactive]
19+
[-o|--output <OUTPUT_DIRECTORY>]
20+
21+
dotnet project convert -h|--help
22+
```
23+
24+
## Description
25+
26+
The `dotnet project convert` command converts a file-based program to a project-based program. This command creates a new directory named for your file, scaffolds a *.csproj* file, moves your code into a file with the same name as the input file, and translates any `#:` directives into MSBuild properties and references.
27+
28+
This makes the transition seamless, from a single file to a fully functional, buildable, and extensible project. When your file-based app grows in complexity, or you simply want the extra capabilities afforded in project-based apps, you can convert it to a standard project.
29+
30+
### Conversion process
31+
32+
The command performs the following operations:
33+
34+
1. Creates a new directory named after the input file (without extension).
35+
2. Generates a *.csproj* file with appropriate SDK and properties.
36+
3. Moves the source code to a file with the same name as the input file.
37+
4. Removes `#:` directives from the source code.
38+
5. Translates `#:sdk` directives: the first `#:sdk` directive becomes the `<Project Sdk="Sdk.Id">` or `<Project Sdk="Sdk.Id/version">` attribute, and any additional `#:sdk` directives become `<Sdk Name="Sdk.Id" />` or `<Sdk Name="Sdk.Id" Version="version" />` elements.
39+
6. Translates `#:package` directives to `<PackageReference>` elements in the project file.
40+
7. Translates `#:property` directives to MSBuild properties in the project file.
41+
8. Sets appropriate MSBuild properties based on the SDK and framework detected.
42+
43+
## Arguments
44+
45+
- `FILE`
46+
47+
The path to the file-based program to convert. The file must be a C# source file (typically with a *.cs* extension).
48+
49+
## Options
50+
51+
- **`--dry-run`**
52+
53+
Determines changes without actually modifying the file system. Shows what would be created or modified without performing the conversion.
54+
55+
- **`--force`**
56+
57+
Forces conversion even if there are malformed directives. By default, the command fails if it encounters directives that cannot be properly parsed or converted.
58+
59+
- [!INCLUDE [interactive](../../../includes/cli-interactive.md)]
60+
61+
- **`-o|--output <OUTPUT_DIRECTORY>`**
62+
63+
Specifies the output directory for the converted project. If not specified, a directory is created with the same name as the input file (without extension) in the current directory.
64+
65+
- [!INCLUDE [help](../../../includes/cli-help.md)]
66+
67+
## Examples
68+
69+
- Convert a file-based program to a project:
70+
71+
```dotnetcli
72+
dotnet project convert app.cs
73+
```
74+
75+
Given a folder containing *app.cs* with the following content:
76+
77+
```csharp
78+
#:sdk Microsoft.NET.Sdk.Web
79+
#:package Microsoft.AspNetCore.OpenApi@10.*-*
80+
81+
var builder = WebApplication.CreateBuilder();
82+
83+
builder.Services.AddOpenApi();
84+
85+
var app = builder.Build();
86+
87+
app.MapGet("/", () => "Hello, world!");
88+
app.Run();
89+
```
90+
91+
Running `dotnet project convert app.cs` results in a folder called *app* containing:
92+
93+
*app/app.cs*:
94+
95+
```csharp
96+
var builder = WebApplication.CreateBuilder();
97+
98+
builder.Services.AddOpenApi();
99+
100+
var app = builder.Build();
101+
102+
app.MapGet("/", () => "Hello, world!");
103+
app.Run();
104+
```
105+
106+
*app/app.csproj*:
107+
108+
```xml
109+
<Project Sdk="Microsoft.NET.Sdk.Web">
110+
111+
<PropertyGroup>
112+
<OutputType>Exe</OutputType>
113+
<TargetFramework>net10.0</TargetFramework>
114+
<ImplicitUsings>enable</ImplicitUsings>
115+
<Nullable>enable</Nullable>
116+
<PublishAot>true</PublishAot>
117+
<PackAsTool>true</PackAsTool>
118+
</PropertyGroup>
119+
120+
<ItemGroup>
121+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.*-*" />
122+
</ItemGroup>
123+
124+
</Project>
125+
```
126+
127+
- Convert a file-based program to a project in a specific output directory:
128+
129+
```dotnetcli
130+
dotnet project convert app.cs --output MyProject
131+
```
132+
133+
## See also
134+
135+
- [dotnet build](dotnet-build.md)
136+
- [dotnet run](dotnet-run.md)
137+
- [dotnet publish](dotnet-publish.md)
138+
- [Tutorial: Build file-based C# programs](../../csharp/fundamentals/tutorials/file-based-programs.md)

docs/core/tools/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ The following commands are installed by default:
7979
- [`package remove`](dotnet-package-remove.md)
8080
- [`package search`](dotnet-package-search.md)
8181
- [`package update`](dotnet-package-update.md)
82+
- [`project convert`](dotnet-project-convert.md) (Available since .NET 10 SDK)
8283
- [`reference add`](dotnet-reference-add.md)
8384
- [`reference list`](dotnet-reference-list.md)
8485
- [`reference remove`](dotnet-reference-remove.md)

docs/navigate/tools-diagnostics/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ items:
194194
href: ../../core/tools/dotnet-package-update.md
195195
- name: dotnet publish
196196
href: ../../core/tools/dotnet-publish.md
197+
- name: dotnet project convert
198+
href: ../../core/tools/dotnet-project-convert.md
197199
- name: dotnet reference add/list/remove
198200
items:
199201
- name: dotnet reference add

0 commit comments

Comments
 (0)