Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a WPF form which has a button and a textbox, when i click the button it needs to open a winform exe which has a list box, the items selected in the listbox should be returned to the WPF Form textbox,

What I have tried:

WPF BUTTON CODE
C#
System.Diagnostics.ProcessStartInfo p = new System.Diagnostics.ProcessStartInfo();
            //System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.UseShellExecute = false;
            p.RedirectStandardOutput = true;
            p.FileName= System.IO.Path.GetDirectoryName(GeneralActivities.ExecutionPath) + "\\MYTESTAPP.exe";

            try
            {
                using (System.Diagnostics.Process startprocess = System.Diagnostics.Process.Start(p))
                {
                    startprocess.WaitForExit();
                    string output = startprocess.StandardOutput.ReadToEnd();
                    
                    MessageBox.Show(output);
                }
            }
Posted
Updated 11-Jun-17 22:14pm
v2

1 solution

Something like this:
C#
using (System.Diagnostics.Process p = new System.Diagnostics.Process())
{
	p.StartInfo = new System.Diagnostics.ProcessStartInfo(System.IO.Path.GetDirectoryName(GeneralActivities.ExecutionPath) + "\\MYTESTAPP.exe");
	p.StartInfo.CreateNoWindow = true;
	p.StartInfo.ErrorDialog = false;
	p.StartInfo.RedirectStandardError = true;
	p.StartInfo.RedirectStandardInput = true;
	p.StartInfo.RedirectStandardOutput = true;
	p.StartInfo.UseShellExecute = false;
	p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
	p.Start();
	System.Threading.Thread.Sleep(10000); // for testing ...

	if (!p.HasExited)
	{
		p.WaitForExit(120000); // wait 2 minutes to finish
		if (!p.HasExited)
		{
			p.Kill();
		}
	}

	string output = p.StandardOutput.ReadToEnd();
}
 
Share this answer
 
v2
Comments
Mohideenmeera 12-Jun-17 7:20am    
Hi Rick,

Thanks for the info.
Do i need to add / modify any code in the exe which is been called from the Other.
If yes, then pls explain the same....
RickZeeland 12-Jun-17 8:34am    
If you want values returned from the exe, YES.
You need to output to the console, which is not possible by default in Winforms, but here is a way to do it: https://www.codeproject.com/Tips/68979/Attaching-a-Console-to-a-WinForms-application
RickZeeland 12-Jun-17 8:41am    
Maybe it's easier to just write the ouput to a file, and read that from your WPF app.
Mohideenmeera 13-Jun-17 12:00pm    
Thanks for your time and response will check by using the same and would keep you posted for the result...
Mohideenmeera 14-Jun-17 6:49am    
Hi Rick,
Found an another solution which worked for my case. Please find the below code
Converted the exe application to DLL and in Form1 [EXE] added this code

public string variable1 { get { return OutList.Items[0].ToString(); } }

In Form2 used the below code to access the variable variable1:

TestNameSpace.TestClass ss= new TestNameSpace.TestClass();
ss.ShowDialog();

MessageBox.Show(ss.variable1.ToString());

Worked as expected.....
Thanks for your time and explanation....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900