68

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.

Tada
  • 1,585
  • 2
  • 16
  • 26

3 Answers3

137

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.

Jeankowkow
  • 814
  • 13
  • 33
yo chauhan
  • 12,079
  • 4
  • 39
  • 58
  • 3
    This is the proper way to do it. See http://msdn.microsoft.com/en-us/library/system.windows.application.startup.aspx – Eugene Aug 02 '12 at 02:41
  • 15
    In addition to the suggestion above, I believe it is worth noting that you need to remove the StartupUri property in the App.xaml if it exists. If you do not, you will spawn two instances of your window. – Tada Aug 02 '12 at 03:03
38

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.

Abed Hawa
  • 1,372
  • 7
  • 18
Habeeb
  • 7,601
  • 1
  • 30
  • 33
  • App_Startup's StartupEventArgs.Args is better. The problem with Environment.GetCommandLineArgs is that it can include some junk, like ".vhost.exe" when running your WPF project from Visual Studio. – Gabe Halsmer Jan 09 '19 at 16:35
0

I Prefer override OnStartup because the Startup event is usually registered in "App.xaml" and I sometimes don't want to modify it. And OnStartup function can provide a way to do some preprocess before Startup event is invoked. This is why we can override OnStartup!

loli
  • 1