简体   繁体   English

使用参数从命令行运行exe

[英]Running exe from command line with parameters

I have created a WPF application which I would like to run from the command line, so I can schedule this command to be executed using Windows Task Scheduler. 我创建了一个WPF应用程序,希望从命令行运行它,因此我可以计划使用Windows Task Scheduler执行该命令。

For example, using a command line: 例如,使用命令行:

start "App.exe" "ID=1"

My questions is, how do I configure my WPF application to handle a call like this and is this the right syntax I should be using from making the call from the command line. 我的问题是,如何配置WPF应用程序来处理这样的调用,这是从命令行进行调用时应该使用的正确语法。

In a WPF application, you can access the command line by using the static members of the Environment class... 在WPF应用程序中,可以使用Environment类的静态成员来访问命令行。

public MainWindow()
        {
            var args = Environment.GetCommandLineArgs();
            if (args.Length == 1)
            {
                MessageBox.Show("No argument provided");
                Environment.Exit(0);
            }
            string arg1 = args[1];  // your argument
            InitializeComponent();
        }

This snippet shows how to do it. 此代码段显示了如何执行此操作。 Remember that the name of the assembly is always the first argument, so you are interested in args[1] and args[2] etc etc. 请记住,程序集的名称始终是第一个参数,因此您对args [1]和args [2]等感兴趣。

The Environment class also has another member: Environment.CommandLine which has the entire command line as a string. Environment类还具有另一个成员:Environment.CommandLine,它将整个命令行作为字符串。

For your second question, your syntax is fine. 对于第二个问题,语法很好。

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

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