Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!!

like image 289
Sana Avatar asked May 11 '12 07:05

Sana


2 Answers

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.

like image 87
Jehof Avatar answered Nov 17 '22 19:11

Jehof


Well I finally found a solution to my question if anyones interested. While in the calling application I maintained the same code I previously used:

ProcessStartInfo BOM = new ProcessStartInfo();
BOM.FileName = @"D:\WPFAPPLICATION.exe";
BOM.Arguments = temp;
Process.Start(BOM);

In the called application in order to receive arguments successfully I simply had to :

    System.Text.StringBuilder strbuilder= new System.Text.StringBuilder();


    foreach (String arg in Environment.GetCommandLineArgs())
    {
        strbuilder.AppendLine(arg);
        barcode = arg;
    }
    psnfo = strbuilder.ToString();

I was not handling the arguments passed to the process in the correct manner

so upon displaying psnfo

The code returns :

 D:\WPFAPPLICATION.exe temp

Source : http://www.codeproject.com/Questions/386260/Using-process-start-in-a-wpf-application-to-invoke

like image 26
Sana Avatar answered Nov 17 '22 21:11

Sana