Nóta
Aðgangur að þessari síðu krefst heimildar. Þú getur prófað aðskrá þig inn eða breyta skráasöfnum.
Aðgangur að þessari síðu krefst heimildar. Þú getur prófað að breyta skráasöfnum.
This article applies to: ✔️ .NET 10 SDK and later versions
Name
dotnet project convert - Converts a file-based program to a project-based program.
Synopsis
dotnet project convert <FILE> [--dry-run] [--force] [--interactive]
[-o|--output <OUTPUT_DIRECTORY>]
dotnet project convert -h|--help
Description
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.
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.
Conversion process
The command performs the following operations:
- Creates a new directory named after the input file (without extension).
- Generates a .csproj file with appropriate SDK and properties.
- Moves the source code to a file with the same name as the input file.
- Removes
#:directives from the source code. - Translates
#:sdkdirectives: the first#:sdkdirective becomes the<Project Sdk="Sdk.Id">or<Project Sdk="Sdk.Id/version">attribute, and any additional#:sdkdirectives become<Sdk Name="Sdk.Id" />or<Sdk Name="Sdk.Id" Version="version" />elements. - Translates
#:packagedirectives to<PackageReference>elements in the project file. - Translates
#:propertydirectives to MSBuild properties in the project file. - Sets appropriate MSBuild properties based on the SDK and framework detected.
Arguments
FILEThe path to the file-based program to convert. The file must be a C# source file (typically with a .cs extension).
Options
--dry-runDetermines changes without actually modifying the file system. Shows what would be created or modified without performing the conversion.
--forceForces conversion even if there are malformed directives. By default, the command fails if it encounters directives that cannot be properly parsed or converted.
-
--interactiveAllows the command to stop and wait for user input or action. For example, to complete authentication.
-o|--output <OUTPUT_DIRECTORY>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.
-
-?|-h|--helpPrints out a description of how to use the command.
Examples
Convert a file-based program to a project:
dotnet project convert app.csGiven a folder containing app.cs with the following content:
#:sdk Microsoft.NET.Sdk.Web #:package Microsoft.AspNetCore.OpenApi@10.*-* var builder = WebApplication.CreateBuilder(); builder.Services.AddOpenApi(); var app = builder.Build(); app.MapGet("/", () => "Hello, world!"); app.Run();Running
dotnet project convert app.csresults in a folder called app containing:app/app.cs:
var builder = WebApplication.CreateBuilder(); builder.Services.AddOpenApi(); var app = builder.Build(); app.MapGet("/", () => "Hello, world!"); app.Run();app/app.csproj:
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net10.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <PublishAot>true</PublishAot> <PackAsTool>true</PackAsTool> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.*-*" /> </ItemGroup> </Project>Convert a file-based program to a project in a specific output directory:
dotnet project convert app.cs --output MyProject