简体   繁体   English

不能正确获取命令行参数输出

[英]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. 我还添加了Console.ReadLine()语句,但无法在For循环内部输入以计算迭代次数。 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. 您的代码还可以,但是请注意,您必须在运行时输入参数,但是由于在运行时调用的第一个方法是Main()因此您没有机会提供命令行参数。 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 ,因此请打开命令行,然后按如下所示运行ConsoleApplication.exe

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. BTW:你总是可以运行使用CTRL + F5代替F5控制台应用程序,因为如果你写了这将产生相同的结果Console.ReadLine()在你的应用程序的结束。

You should move the Console.ReadLine() statement at the end of your Main method. 您应该在Main方法的末尾移动Console.ReadLine()语句。 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. 从控制台运行程序时,除去console.readline()语句,而是提供输入。 I have no idea about c# but in java you can achieve the above task as follow . 我对c#一无所知,但是在Java中,您可以按以下步骤完成上述任务。

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. 实际上 ,您不需要使用Console.ReadLine()因为它读取输入流,而不是您提供的参数。

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 Run并键入cmd.exe ,这是命令行的快捷方式
  • Go to your exe folder with cd (in my example cd C:\\Users\\Soner\\Documents\\Visual Studio 2012\\Projects\\ProgramConsole\\ProgramConsole\\bin\\Debug ) cd转到您的exe文件夹(在我的示例中为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 ) 使用名称和ABCD运行exe(在我的示例ProgramConsole.exe ABCD

Output will be; 输出将是;

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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