Question

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.

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.

Was it helpful?

Solution

In a WPF application, you can access the command line by using the static members of the Environment class...

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.

The Environment class also has another member: Environment.CommandLine which has the entire command line as a string.

For your second question, your syntax is fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top