82

Now that .NET 6.0 is out, what appears to have be a radical update to the default CLI project template is the absence of the familiar boilerplate being reduced to the following:

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

What is not clear (and I have been trying to find documentation thus far, to no avail) is how does one access the command-line arguments passed to the executable's entrypoint class?

djtubig-malicex
  • 1,018
  • 1
  • 7
  • 11
  • Just because explicitly specifying the `Main` method isn't needed anymore (because it's added automatically in the background) it doesn't mean it's not allowed ... – derpirscher Nov 28 '21 at 10:48
  • Just follow the link in the comment, it spells out what to do when you need Main(). – Hans Passant Nov 28 '21 at 10:55
  • 1
    The intent of the question is to assist those who wish to embrace the new way of working with such project templates, for lack of clarity in documentation due to it being 'bleeding edge'. Using the original template basically means "don't use the new template" which is a regressive approach. – djtubig-malicex Nov 28 '21 at 11:03
  • 3
    @HansPassant The link does not document the alternate way of accessing CLI arguments with the new template. My question is not asking for how to use `Main()`. – djtubig-malicex Nov 28 '21 at 11:09
  • 2
    Ridiculous move by MS team, loosing the beauty of C#, and making things not so obvious - where's the namespace, is it a plain main() or async main(), etc etc. – joedotnot Sep 20 '22 at 16:17
  • @joedoenot To be fair, I would question the need of an `async Main()` seeing as though most executable process entrypoints are on the main process thread. Who's going to be `await`-ing? – djtubig-malicex Sep 26 '22 at 23:21

4 Answers4

110

You can access the command line arguments from anywhere in your code using the Environment class.

In particular, you can use Environment.GetCommandLineArgs:

string name = Environment.GetCommandLineArgs()[1];
Console.WriteLine($"Hello, {name}!");

Note that the first element in the array contains the path of the executable and the arguments passed to the program start with the second element, i.e. at index 1.

Codo
  • 75,595
  • 17
  • 168
  • 206
64

New project templates are using C# 9 feature called top-level statements.

For top-level statement file compiler will generate string[] args parameter (actually it generates the whole class containing Main method), so you can just use it (as previously was done with Main(string[] args)):

Console.WriteLine(args.Length);
Console.WriteLine(args.FirstOrDefault());

More info about the generation patterns can be found in the Top-level statements feature spec. Also see the Top-level statements - programs without Main methods doc.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
2

Little late to the game but there is a default setup already for this in .NET 6. Use 'args' that is built in. It's pretty much the exact old school

public static void Main(string[] args)

Just giving giving you 'args' as a variable that's just there for reuse. I actually just found it on accident seeing the 'locals' pop up on VS 2022.

If I want to debug test a console app just use the 'launchSettings.json' under profiles>ConsoleApp>commandLineArgs.

So if I have in a startup:

{
  "profiles": {
    "ConsoleApp": {
      "commandName": "Project",
      "commandLineArgs": "Brett Robel"
    }
  }
}

And in my .NET 6 command line app:

var firstName = args.First();
var lastName = args.Last();
Console.WriteLine($"Hello {firstName} and Last {lastName}");

I should see:

enter image description here

djangojazz
  • 14,131
  • 10
  • 56
  • 94
  • 1
    Wow, discovering how to use a new feature by "found it on accident seeing the 'locals' pop up" is not a well thought out way to deliver new features MS! MS needs to do better – Jason Doyle Jul 30 '23 at 18:08
  • on json file - how to get the "commandName" value from console ? – dr.Xis Aug 02 '23 at 10:19
0

In Progam.cs you can just get the parameters from the args[] array

bartburkhardt
  • 7,498
  • 1
  • 18
  • 14