How to start WPF based on Arguments

C#Wpf

C# Problem Overview


I'm currently developing an application that does some file manipulation and I want to be able to do the manipulation through the console or via an UI (I chose WPF).

I pretty much want to say: (psuedo)

if ( Environment.GetCommandLineArgs().Length > 0 )
{
    //Do not Open WPF UI, Instead do manipulate based
    //on the arguments passed in
}
else
{
    //Open the WPF UI
}

I've read about a few different ways of starting the WPF Window/application programmatically like:

Application app = new Application ();
app.Run(new Window1());

But I'm not entirely sure I want to just plug this into a Console Application.

Does anyone have best practices or recommendations on how I can achieve this? The main processing functionality is in a Helper class I created. So basically I either want a static start method (like standard Console Application creates) or the UI to access the Helper class depending on the arguments passed in.

C# Solutions


Solution 1 - C#

In Application class there is an event "StartUp" you can use it . It provide you the args you provide through command prompt. Here is an example from MSDN:

App.xaml

<Application x:Class="WpfApplication99.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Startup="App_Startup">
</Application>

App.xaml.cs

public partial class App : Application
{
    void App_Startup(object sender, StartupEventArgs e)
    {
        // Application is running
        // Process command line args
        bool startMinimized = false;
        for (int i = 0; i != e.Args.Length; ++i)
        {
            if (e.Args[i] == "/StartMinimized")
            {
                startMinimized = true;
            }
        }

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

I hope this will help.

Solution 2 - C#

There are 2 options to get the command line arguments

  1. If you want to read the arguments OnStartup. This is good for global access of the args.

Override OnStartup in App.xaml.cs and look at the Args property of the StartupEventArgs class.

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        foreach (string arg in e.Args)
        {
            // TODO: whatever
        }
        base.OnStartup(e);
    }
}

2) Another easy way is to read the arguments from the Environment Object.

Environment.GetCommandLineArgs();
This can be used from anywhere in the application like from the Form / Page also.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTadaView Question on Stackoverflow
Solution 1 - C#yo chauhanView Answer on Stackoverflow
Solution 2 - C#HabeebView Answer on Stackoverflow