Search code examples
c#wpfcommand-line-argumentscustom-action

WPF can't read arguments passed


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


Solution

  • 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