Using process.start in a wpf application to invoke another wpf application

Sana picture Sana · May 11, 2012 · Viewed 9.8k times · Source

I am trying to invoke one wpf application from another wpf application. The invoking wpf application makes the call

ProcessStartInfo BOM = new ProcessStartInfo();

BOM.FileName = @"D:\WPFAPPLICATION.exe";

BOM.Arguments = temp;

Process.Start(BOM);

Now in the invoked application, I try to retrieve the argument passed using

  string arguments =Process.GetCurrentProcess().StartInfo.Arguments;

However the arguments are not passed. why is this??

I also tried an alternative method where in:

    public partial class App : Application
    {
    public static String[] mArgs;

    private void Application_Startup(object sender, StartupEventArgs e)
    {

        if (e.Args.Length > 0)
        {
            mArgs = e.Args;


        }
    }
    }
    }

However this did not work either!!! Please HELP!!

Answer

Jehof picture Jehof · May 11, 2012

Try using the Environment class to get the commandline arguments.

string[] args = Environment.GetCommandLineArgs

or use the string[] that is passed to your main method of your WPF Application (App.xaml.cs).

public partial class App : Application {

    protected override void OnStartup(StartupEventArgs e) {
        string[] args = e.Args;
    }
}

Note: The call

string arguments =Process.GetCurrentProcess().StartInfo.Arguments;

will not return any value. See this MSDN entry

If you did not use the Start method to start a process, the StartInfo property does not reflect the parameters used to start the process. For example, if you use GetProcesses to get an array of processes running on the computer, the StartInfo property of each Process does not contain the original file name or arguments used to start the process.