|
| 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) |
0 commit comments