100

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?

6
  • 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 ... Commented Nov 28, 2021 at 10:48
  • Just follow the link in the comment, it spells out what to do when you need Main(). Commented Nov 28, 2021 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. Commented Nov 28, 2021 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(). Commented Nov 28, 2021 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
    Commented Sep 20, 2022 at 16:17

6 Answers 6

127

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.

2
  • 13
    I was checking and this work too: Console.WriteLine($"Hello, World! {args[1]}");
    – Peres
    Commented Feb 24, 2022 at 14:22
  • 2
    @Peres Yes, that's Guru Stron's answer
    – Codo
    Commented Feb 24, 2022 at 14:28
78

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.

6
  • 2
    Doesn't compile as args is not defined. Commented Nov 28, 2021 at 11:06
  • 6
    @djtubig-malicex I have it compiling on my machine, at shraplab.io (one of the links in the answer) and here is dotnetfiddle.
    – Guru Stron
    Commented Nov 28, 2021 at 11:08
  • 4
    Why does this feel pythonic?
    – IAbstract
    Commented Jan 16, 2022 at 23:29
  • 1
    @IAbstract even in python you still need to explicitly import sys to access sys.argv. Commented May 18, 2022 at 1:40
  • 1
    Works fine for me, thanks :) args.ToList().ForEach(x => Console.WriteLine($"{x}!")); Commented Sep 23, 2022 at 7:16
7

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

3
  • 2
    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 Commented Jul 30, 2023 at 18:08
  • on json file - how to get the "commandName" value from console ?
    – dr.Xis
    Commented Aug 2, 2023 at 10:19
  • 1
    In Visual Studio 2022, if you do not see a launchSettings.json file under Properties, right click the console project and left click properties. Type the word "launch" in the search bar, which will filter you down to the Debug > General. Now left click "Open debug launch profiles UI" and type in your args in the "Command line arguments" text box. There is no save button, so just close the dialog afterward by click the X on it. This will create it for you. Commented May 30 at 21:59
2

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

0

If you want to complicate your life, there is a Command-line configuration provider that can be used with Microsoft.Extensions.Configuration.

-5

You are referring to Top-level statements which Microsoft introduced in C# language version 9 to mimic Python's plain syntax in an effort to rival their appeal especially to novice programmers.

Top-level statements can take input arguments like so:

using System;

string[] args = new string[1];

Console.WriteLine($"Hello {args[0]}");

If you compile the above C# code to Program.exe and navigate to its folder, from there you can then simply call it and pass it an input argument from the command line like so:

Program.exe world
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.