控制台应用程序Args [] [英] Console application Args[]

查看:90
本文介绍了控制台应用程序Args []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制台应用程序需要通过命令行由外部进程触发。当我运行应用程序并在弹出的命令行界面中输入参数时,除非我执行readline,否则args {}总是空的( )声明。他们在我们的自动化中不会有用户交互。有什么想法?

I have a console application that needs to triggered by an external process via command line .When I run the application and enter parameters in the command line screen that pops up, the args{} is always empty unless I do a readline() statement.They will be no user interaction in our automation. Any ideas?

推荐答案

这不是这些参数的工作原理。你没有显示你的代码,所以我不知道你是如何组织你描述的输入的,但是,不管你怎么做,它都不会影响 Main 。这些是运行应用程序之前输入的命令行的参数,用于启动应用程序的命令行参数。此外,此命令行与控制台无关(弹出的命令行界面)。用户可以通过许多不同的方式进入命令行。带命令行的控制台只是其他应用程序的控制台;它可以是任何其他应用程序:CMD.EXE,资源管理器或您编写的应用程序。在所有情况下,父进程通过将OS路径传递给主可执行模块和可选命令行参数来创建一个新进程(有问题的应用程序)。



如果您启动应用程序,例如

This is not how those parameters work. You did not show your code, so I don't know how you organized the input you described, but, no matter how you did it, it could not effect the parameters of Main. Those are the parameters of the command line entered before you run the application, the parameters of the command line which was used to start the application. Moreover, this command line has nothing to do with console ("command line screen that pops up"). A use can enter command line in many different ways. Console with command line is just the console of some other application; it could be any other application: "CMD.EXE", Explorer, or application you wrote. In all cases, the parent process creates a new process (of the application in question) by passing the OS the path to the main executable module and optional command line parameters.

If you start the application like
MyApplication.exe



您将获得空命令行。如果您使用命令启动应用程序


you get empty command line. If you start your application with command

MyApplication.exe "my text" -r myFile



您的入口点 Main 方法获取一个参数,即一个包含3个元素的字符串数组:my text, - r和myFile。请注意,第一个参数不会被其内部空格分割,因为.NET还会预解析命令行来处理报价。



您还可以获得原始参数 一行中的命令行: http://msdn.microsoft.com/en-us/library /system.environment.aspx [ ^ ]。



你也可以获得解析形式的命令行参数,不同于传递给 Main ,当应用程序的主可执行模块的完整路径包含在数组的索引0时: http://msdn.microsoft.com/en-us/library/system.environment.getcommandlineargs.aspx [ ^ ]。



要解析应用程序的命令行参数,请参阅我的文章:基于枚举的命令行实用程序 [ ^ ]。



< dd> -SA


your entry point Main method gets a parameter, which is a string array with 3 elements: "my text", "-r" and "myFile". Note that first parameter is not split by its inner blank space, because .NET additionally "pre-parse" command line to handle quotations.

You can also get "raw" command line in one string: http://msdn.microsoft.com/en-us/library/system.environment.aspx[^].

And also you can get command line arguments in parsed form different from that passed to Main, when the full path of the main executable module of the application is included at index 0 of the array: http://msdn.microsoft.com/en-us/library/system.environment.getcommandlineargs.aspx[^].

For parsing command line argument by the application, please see my article: Enumeration-based Command Line Utility[^].

—SA


您可以看到以下代码示例



The following code sample you can see

namespace Tests
{
    class Program
    {
        static void Main(string[] args)
        {
            if (null != args && args.Length > 0)
            {
                foreach (var s in args)
                {
                    System.Console.WriteLine(s);
                }
            }
        }
    }
}





执行来自命令提示符的控制台应用程序





Execute the console application from command prompt

C:\test>Tests.exe 1 2 "Hello Good Morning" 4 5 Hello Hi





输出是

1

2

3

你好早安
4

5

你好

您好



the output is
1
2
3
Hello Good Morning
4
5
Hello
Hi


Args参数不关心用户输入 程序启动后 - 它关注用户输入 程序启动时 - 即在从命令行(或替代)执行程序时给出。

如果调出命令提示符(通过按开始按钮并键入C MD然后输入)然后您可以移动到保存应用程序的目录类型

The Args parameter is not concerned with user input after the program has been started - it is concerned with user input when the program is started - i.e. given at the time the program is executed from a command line (or substitute).
If you call up a command prompt (by pressing the start button and typing CMD then ENTER) you can then move to the directory holding you application at type
MyApp Param1 Param2

Windows将运行您的应用程序,并通过通过Args数组的两个字符串:Param1和Param2。

Windows will run your application, and pass two strings via the Args array: "Param1" and "Param2".


这篇关于控制台应用程序Args []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆