简体   繁体   中英

Not Getting the Command Line Parameter Output Properly

I am trying to display the Number of Command Line Arguments entered as an input. Here is the block of my code.

//Argument: A, B, C, D

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ParamaterCount
{
    public class ParameterCount
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World");
            Console.WriteLine("You have entered {0} command line arguments",args.Length);
            Console.ReadLine();
            for (int i = 0; i < args.Length; i++)
            {
                Console.WriteLine("{0}", args[i]);
            }
        }
    }
}

However when I am trying to run it. It gets out of the screen and I am getting nothing. I have also added the Console.ReadLine() statement but I am not able to enter inside the For Loop to count the iteration. Am I missing something ? Thanks.

Output Should be like this.

Hello World,
You entered 4 Command Line Arguments
A
B
C
D

In a console application arguments won't work like that. Your code is pretty OK, but note that you have to input the arguments at the run time, but since the first method which is called at the runtime is Main() so you haven't had the chance to provide the command line arguments. For achieving what you want, you have to run your compiled output application from command line, lets say your application name is ConsoleApplication.exe , so open the command line an then run ConsoleApplication.exe like this:

ConsoleApplication.exe A B C …

For further information see this: Command-Line Arguments

BTW: You can always run your console application using ctrl + F5 instead of F5 , this will produce the same result as if you wrote a Console.ReadLine() at the end of your application.

You should move the Console.ReadLine() statement at the end of your Main method. So your code should look like this:

public static void Main(string[] args)
{
    Console.WriteLine("Hello World");
    Console.WriteLine("You have entered {0} command line arguments",args.Length);

    for (int i = 0; i < args.Length; i++)
    {
        Console.WriteLine("{0}", args[i]);
    }
    Console.ReadLine();
}

Try:

 public static void Main(string[] args)
{
    Console.WriteLine("Hello World");
    Console.WriteLine("You have entered {0} command line arguments {1}",args.Length, string.Join(" ", args);

    Console.ReadLine();
}

Remove the console.readline() statement, instead Provide input while you are running your program from console. I have no idea about c# but in java you can achieve the above task as follow .

javac s.java
java s A B C D

Actually , you don't need to use Console.ReadLine() because it reads input stream, not arguments that you provided.

That's the code you just need;

Console.WriteLine("Hello World");
Console.WriteLine("You have entered {0} command line arguments", args.Length);
for(int i = 0; i < args.Length; i++)
{
    Console.WriteLine("{0}", args[i]);
}

Then follow these steps;

  • Go Run and type cmd.exe which is shortcut for command line
  • Go to your exe folder with cd (in my example cd C:\\Users\\Soner\\Documents\\Visual Studio 2012\\Projects\\ProgramConsole\\ProgramConsole\\bin\\Debug )
  • Run your exe with it's name and with ABCD (in my example ProgramConsole.exe ABCD )

Output will be;

Hello World
You have entered 4 command line arguments
A
B
C
D

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM