-1

I am just getting started with some basic C# Exercise. I am referring the examples from the below link

https://msdn.microsoft.com/en-us/library/aa288457(v=vs.71).aspx

Here is the code below

// cmdline2.cs
// arguments: John Paul Mary
using System;

public class CommandLine2
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Number of command line parameters = {0}",
       args.Length);
        Console.ReadLine();
        foreach (string s in args)
        {
            Console.WriteLine(s);
        }
    }
}

Below is the expected outcome.

Run the program using some arguments like this: cmdline2 John Paul Mary.

The output will be:

Number of command line parameters = 3
John
Paul
Mary

However in my case first when I tried to execute the code, the command line is appearing for a second and getting disappeared.

I added console.read() and I am seeing below.

Number of command line parameters = 0

So I wanted to understand what's going wrong out here. Any help is appreciated.

Regards Anurag

Raktim Biswas
  • 3,833
  • 5
  • 22
  • 29
Aron
  • 1
  • 1
  • Are you sure you're passing the arguments properly? Your code looks fine – Emile Pels Aug 06 '16 at 14:10
  • 1
    How are you running the app? If you're running with the debugger in Visual Studio then it's probably launching it with no command line arguments. You can either run it by hand from a DOS prompt or set the command line parameters in the Debug panel of the project Properties window (right-click project in Solution Explorer, Properties). Leave the Console.Read in there to prevent it from exiting immediately from debug mode. – James McLachlan Aug 06 '16 at 14:15
  • It's what James is saying. You need to open a separate command window. Go into the directory your build is in and type in your command there. You can can Shift-Right click the "build" folder and choose Command Prompt Here. – Mastro Aug 06 '16 at 16:07
  • See also https://stackoverflow.com/questions/27556999/how-to-see-output-of-a-c-sharp-console-program-when-running-in-vs, https://stackoverflow.com/questions/11512821/how-to-stop-c-sharp-console-applications-from-closing-automatically, https://stackoverflow.com/questions/16952846/how-to-keep-console-window-open, https://stackoverflow.com/questions/8868338/why-is-the-console-window-closing-immediately-without-displaying-my-output – Peter Duniho Aug 06 '16 at 18:15

2 Answers2

0

You must add the readline, at the end, to pause execution, otherwise the program just runs, and exits...

// cmdline2.cs
// arguments: John Paul Mary
using System;

public class CommandLine2
{
   public static void Main(string[] args)
   {
       Console.WriteLine(
         "Number of command line parameters = {0}",
          args.Length);
       foreach (string s in args) Console.WriteLine(s);

       Console.WriteLine("Hit any key to exit");
       Console.ReadLine();
   }
}

ALso, to run this from Visual Studio, add your command line arguments from the properties page for the project:

enter image description here

Charles Bretana
  • 131,932
  • 22
  • 140
  • 207
0

args is an array for commands passed into the executable.

What you aren't showing is where you are passing in the command line arguments. This can be done two ways:

  1. By calling the exe from the command line, for example ping www.google.com -t calls ping.exe with 2 arguments.

in your example you would call commandLine2.exe 3 John Paul Mary

  1. Debugging you can use the following to specificy commands:

enter image description here

In the Start options you need to add your command line arguments.

You should then see the correct number of arguments.

The last item is your readline is before the Console.WriteLine(). Move your read to the end of your code, inside of the Main method.

Austin T French
  • 4,373
  • 1
  • 18
  • 37