jozef
jozef

Reputation: 101

dotnet new console --use-program-main does not work

when you are in VS Code you start a new console, the program code generated has the new format where there is no main() section as in the old days. I like have the main() section. I should be able to type in dotnet new console --use-program-main

but when I do I get

Error: Invalid option(s): --use-program-main '--use-program-main' is not a valid option I have tried reporting this to microsoft who said it was a VS Code issue and I'd have to report it on github. I tried that but haven't gotten a response, but I'm not sure I reported it in the correct way and githup also suggested I post it on stack overflow so here I am. I am using Visual Studio Code 1.87.2 on a Dell Precision 3530 laptop running Win 10 pro with recent updates. I've tried some variations of hyphens and spaces but nothing seems to work.

Upvotes: 0

Views: 2141

Answers (1)

It all makes cents
It all makes cents

Reputation: 5009

It's unclear what version of .NET you're targeting. Neither Visual Studio Code nor Visual Studio are required to create a .NET Console App. One can create a new project by simply opening a cmd window and using dotnet new .

To see installed SDKs:

  • dotnet --info

To see available templates

  • dotnet new list

You've indicated that you desire to have a main method.

According to .NET default templates for dotnet new

--use-program-main

If specified, an explicit Program class and Main method will be used instead of top-level statements. Available since .NET SDK 6.0.300. Default value: false. Available only for C#.

Create a new Console App project (name: Test123):

  • dotnet new console --use-program-main --framework <TargetFramework> --output <fully-qualified path>

For example, to create a new project in a subfolder of your Documents folder (ex: %UserProfile%\Documents\Projects)

Note: Below you'll notice two subdirectories with the same name (Test123). The first one is for the solution and the subfolder with the same name is for the project - this is the same structure Visual Studio creates which means that your solution/project will be usable by both Visual Studio Code and Visual Studio.

 dotnet new console --use-program-main --framework net8.0 --output "%UserProfile%\Documents\Projects\Test123\Test123"

Note: According to .NET default templates for dotnet new

--use-program-main

If specified, an explicit Program class and Main method will be used instead of top-level statements. Available since .NET SDK 6.0.300. Default value: false. Available only for C#.

Create solution file:

Note: See dotnet sln for more information.

dotnet new sln --name "Test123" --output "%UserProfile%\Documents\Projects\Test123" --project "%UserProfile%\Documents\Projects\Test123\Test123\Test123.csproj"

Add Project to Solution File:

dotnet sln "%UserProfile%\Documents\Projects\Test123" add "%UserProfile%\Documents\Projects\Test123\Test123\Test123.csproj"

Optional - Add NuGet package

If desired, search for desired NuGet package, for example NuGet package Microsoft.Data.SqlClient

Download and add desired NuGet package to project (ex: Microsoft.Data.SqlClient)

dotnet add "%UserProfile%\Documents\Projects\Test123\Test123\Test123.csproj" package "Microsoft.Data.SqlClient"

or

dotnet add "%UserProfile%\Documents\Projects\Test123\Test123\Test123.csproj" package "Microsoft.Data.SqlClient" --source "https://api.nuget.org/v3/index.json"

See dotnet add package, dotnet nuget add source, and Common NuGet configurations for more information.


To Build Application:

dotnet build "%UserProfile%\Documents\Projects\Test123\Test123.sln"

To Run Application:

dotnet run --project "%UserProfile%\Documents\Projects\Test123\Test123\Test123.csproj"

Use your favorite editor to develop/modify the Console App. Both Visual Studio and Visual Studio Code are available here. However neither is necessary. If desired, one can open/modify the files using Notepad.


Additional Resources:


Here's a PowerShell script that one can use to create a .NET solution/project -- it uses the commands above.

CreateDotNetProject.ps1

param (
    [string]$location = $env:UserProfile + "\Documents\Projects",
    [Parameter(Mandatory=$true)][string]$solutionName,
    [string]$projectName = $solutionName,
    [string]$framework="",
    [string]$package="",
    [string]$packages="",
    [string]$packageVersion=""

 )

 Write-Host "Location:  $($location)"
 Write-Host "Solution name:  $($solutionName)"
 Write-Host "Project name:  $($projectName)"

 $solutionFolder = $location, $solutionName -join "\"
 Write-Host "Solution Folder:  $($solutionFolder)"

 $solutionFilename = $solutionFolder, ($solutionName + ".sln") -join "\"
 Write-Host "Solution Filename:  $($solutionFolder)"

 $projectFolder = $location, $solutionName, $projectName -join "\"
 Write-Host "Project Folder:  $($projectFolder)"

 $projectFilename = $projectFolder, ($projectName + ".csproj") -join "\"
 Write-Host "Project Filename:  $($projectFile)"
 Write-Host 

 #create console project
 Write-Host "Creating Console App '$($projectName)'..."

 if ($framework -ne "")
 {
   dotnet new console --use-program-main --framework $framework --output $projectFolder
 }
 else
 {
   dotnet new console --use-program-main --output $projectFolder
 }

 #create solution
 Write-Host "Creating solution '$($solutionName)'..."
 dotnet new sln --name $solutionName --output $solutionFolder --project $projectFilename

 #add project to solution
 Write-Host "Adding project '$($projectName)' to solution..."
 dotnet sln $solutionFolder add $projectFilename

 Write-Host

 #if -Packages is specified, don't process anything specified with -Package
 if ($packages -ne "")
 {
    #if specified, process each package name in comma-delimited string
    Write-Host "Packages: $($packages)"
    Write-Host
    $packages.Split(",") | ForEach-Object -Process {
      Write-Host "Adding package '$($_)'..."
      dotnet add $projectFilename package $_ --source "https://api.nuget.org/v3/index.json"
   }
 }
 elseif ($package -ne "")
 {
   #if -Package is specified, add package

   Write-Host "Package: $($package)"

   if ($packageVersion -ne "")
   {
      Write-Host "Adding package '$($package)' (version $($packageVersion))..."
      dotnet add $projectFilename package $package --source "https://api.nuget.org/v3/index.json" --version $packageVersion
   }
   else
   {
       Write-Host "Adding package '$($package)'..."
      dotnet add $projectFilename package $package --source "https://api.nuget.org/v3/index.json"
   }
  
 }

Write-Host "Building solution..."
dotnet build $solutionFilename

Write-Host "Publishing..."
dotnet publish $solutionFilename --use-current-runtime

#Write-Host "Running..."
#dotnet run --project $projectFilename

Usage:

  • Open PowerShell
  • Change directory to the folder containing the PowerShell script (ex: cd "PowerShell Scripts")
.\CreateDotNetProject.ps1 -SolutionName:Test123 -Package:Microsoft.Data.SqlClient

Resources:

Upvotes: -1

Related Questions