-1

I am trying to get the command line arguments in WPF but it always returns []. It's very strange I even tried to send argument via cmd but no success

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        MessageBox.Show(e.Args.ToString()); // returns Empty Array
        MessageBox.Show(Environment.GetCommandLineArgs().ToString()); // returns Empty Array

        if (e.Args.Length == 1 && e.Args[0] == "INSTALLER")
        {
            return;
        }

        // Create main application window, starting minimized if specified
        MainWindow mainWindow = new MainWindow();
        mainWindow.WindowState = WindowState.Maximized;
        mainWindow.Show();
    }

This is how I am passing Argument in CustomAction::

enter image description here

#Edit:: I'm using simple VisualStudio installer, generating MSI files. I want to run App after installation with some Arguments

CodingWithRoyal
  • 1,006
  • 1
  • 6
  • 15
  • 2
    Your message box shows "[]"? I'd expect "System.String[]", because ToString returns the name of the type by default. – SomeBody Feb 08 '21 at 06:28
  • @SomeBody I mean to say it return `Empty Array` but it should show some `arg` – CodingWithRoyal Feb 08 '21 at 06:30
  • What exactly is CustomAction? What type of installer do you use? Wix? – user2250152 Feb 08 '21 at 06:38
  • @user2250152 see #Edit in ques.. I want to run the app after installation with Arg – CodingWithRoyal Feb 08 '21 at 06:40
  • 4
    I strongly doubt that `e.Args.ToString()` returns "Empty Array" – Klaus Gütter Feb 08 '21 at 06:54
  • It seems a lot more likely to me that you are misinterpreting what is displayed by your `ToString()` calls, than that the API you're using is fundamentally broken. See duplicate for the correct way to display the contents of an array. Next time, consider using the debugger to actually _examine_ the object directly, rather than assuming that your own code written by you is correct. – Peter Duniho Feb 08 '21 at 07:47

1 Answers1

1

You can override the OnStartup Method in App.cs, which has StartupEventArgs as a parameter. Then just get the commandline arguments with startupEventArgs.Args, like so:

protected override void OnStartup(StartupEventArgs startupEventArgs)
{
    MessageBox.Show(startupEventArgs.Args[0].ToString());
}

If you just print startupEventArgs.Args.ToString() to a Messagebox, it will print System.String[]. This is just the datatype of the array, but that doesn't mean its empty. Try to access it with a index or print startupEventArgs.Args.Length

NoConnection
  • 765
  • 7
  • 23