Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

run an exe from C# application 2

Status
Not open for further replies.

timothy

Programmer
Mar 20, 2000
39
US
I have an older product from Sybase that I need to fire off from a c# application. Normally the exe would be run from dos with some args passed in. How can I fire off this program from C#? I haven't found any docs on the subject. I'm sure there must be someone else that has had to deal with this.

Thanks for your help.



 
Take a look at System.Diagnostics.Process. I've used it to run some old FoxPro exe's and it works fine. I've never had to pass arguements though, so I'm not sure about that.
 
Look into the Process class in the System.Diagnostics namespace, e.g.,

using System.Diagnostics;


Process myProcess = new Process();
myProcess.StartInfo.FileName = {executable filename here};
myProcess.Start();
myProcess.Close();


myProcess.StartInfo has several useful properties you might want to check out.
 
Here is a complete example:
Code:
System.Diagnostics.Process P = new System.Diagnostics.Process();
//P.StartInfo.RedirectStandardOutput = true;
P.StartInfo.CreateNoWindow = true;
P.StartInfo.WorkingDirectory = WorkingDirectory;
P.StartInfo.FileName =  WorkingDirectory + "\\" + Myexe;
P.StartInfo.Arguments = cm[0];						
P.EnableRaisingEvents = true;
P.StartInfo.UseShellExecute = false;   
P.Exited += new EventHandler(captureStdout); // if you want to capture the output when the process exits
try
{
	P.Start();
}
catch(Exception exProcess)
{
LogEvent(" Error in starting process: WorkingDirectory: " + WorkingDirectory + " Exe:" + Myexe + " Arguments: " + cm[0] + " " + exProcess.Message + "\r\n",false);
		return;
}

private void captureStdout(object sender, EventArgs e) 
{      
System.Diagnostics.Process ps = (System.Diagnostics.Process) sender;      
{
	LogEvent(Myexe + " has ended. [" + DateTime.Now.ToString() + "]\r\n",false);
}
-obislavu-
 
Thanks for your help! I completed the project yesterday using your suggestion.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top