如何为“dotnet new”设置变量?

Per*_*idi 3 .net c#

标准命令模板

dotnet new console

包含 #if 用于预处理器:

#if (csharpFeature_TopLevelProgram)
// See https://aka.ms/new-console-template for more information
#endif
#if (!csharpFeature_ImplicitUsings)
using System;

#endif
#if (csharpFeature_TopLevelProgram)
Console.WriteLine("Hello, World!");
#else
namespace Company.ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}
#endif
Run Code Online (Sandbox Code Playgroud)

我应该指定什么命令行开关,dotnet new以便不定义变量“csharpFeature_TopLevelProgram”,这样当我执行dotnet new console该文件时,不是为Net6生成文件,而是像以前在旧的Net5中那样没有TopLevelProgram?

Dav*_*idG 5

如果您使用-h命令行选项,它会告诉您如何使用模板以及您可以指定参数--use-program-main

在此输入图像描述

例如:

dotnet new console --use-program-main true
Run Code Online (Sandbox Code Playgroud)

注意:您可能需要更新到较新版本的 .NET 6 才能使此命令行选项存在。这是用 v6.0.401 测试的