-1

I'm trying to implement a way for me to start my WPF application with specific arguments through the Windows task schedular and CMD. I've added the code below.

protected override void OnStartup(StartupEventArgs e)
        {

            Logger.Info(e.Args.Length);

            for (int i = 0; i != e.Args.Length; ++i)
            {
                if (e.Args[i] == "test")
                {
                    Logger.Info($"G");
                }
                else
                {
                    Logger.Info($"B");
                }
            }
}

When I start publish the application and start it through CMD or schedule it in task schedular with arguments, the e.Args.Length is 0. But when I add an argument in Properties > Debug > Command line arguments, it does work. Any idea what I'm missing?

Jason Lie
  • 159
  • 3
  • 12

3 Answers3

1

You need to use Environment class in System Namespace and use GetCommandLineArgs() method to retrieve the arguments.

For example,

Args = Environment.GetCommandLineArgs();
Abin
  • 2,868
  • 1
  • 23
  • 59
1

According to the Comments of the Question i assume the problem is that the Start-Arguments are passed wrong. A ClickOnce Application doesent work like a .exe File and can not be started with parameters from the CMD due to security reasons enter image description here (Source)

I suggest you take a look at this, it describes nicely how arguments can be passed to the application via query strings.

LittleBit
  • 1,076
  • 6
  • 14
0

For runining the application form cmd with command arguments, use below format -

C:\StackOverflow\Bin\Debug> StackOverflow.exe arg1 arg2
Sham
  • 910
  • 8
  • 15
  • I did, but it still doesn't work. It's not an .exe but an .application. Does this matter? – Jason Lie Oct 02 '18 at 13:11
  • @JasonLie Is it a clickonce application? Did the application launch at all or you got any error? Could you please share the command? – Sham Oct 02 '18 at 13:13
  • The command is just 'Program.application test'. It does launch but when I print the length of the array of arguments, it says 0 – Jason Lie Oct 02 '18 at 13:55
  • Any way I can change it to .exe when publishing? – Jason Lie Oct 03 '18 at 07:22
  • @JasonLie How are you creating that `.Application` file? As per my knowledge, it get created through `ClickOnce` and there would be a different mechanism to send argument to a `ClickOnce` application. – Sham Oct 03 '18 at 08:47
  • I went to Build > Publish – Jason Lie Oct 03 '18 at 08:58
  • @JasonLie Yes, that option is for creating clickonce application. You would need different mechanism to send argument to a clickonce application. Kindly refer the link https://learn.microsoft.com/en-us/visualstudio/deployment/securing-clickonce-applications?view=vs-2017#pass-arguments for more details on it. – Sham Oct 03 '18 at 09:23