Bozhidar Stoyneff
Bozhidar Stoyneff

Reputation: 3634

How to access command line arguments with new Console App template in VS2022

I wonder how one can access the command line arguments, given this is all that gets generated in the Program.cs file by the new template for Console Application in Visual Studio 2022:

// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

Moreover, there is no args variable in scope.

So how to access CLI arguments?

Upvotes: 6

Views: 6096

Answers (2)

Csaba Toth
Csaba Toth

Reputation: 10729

I prefer the old style and check the "Do not use top-level statements" while creating the project:

enter image description here

This way you'd have the good old main with the explicit arguments. You can also possibly add the scaffolding after the fact.

Upvotes: 2

Bozhidar Stoyneff
Bozhidar Stoyneff

Reputation: 3634

Apparently one needs to add some code to fireup the IntelliSense. I.e., if you try to access args as a first thing in your program you'll be supprised (like myself):

No args shown in IntelliSense

args causes compile error


However, couple of lines later:

args appear

Voila!

Upvotes: 8

Related Questions