Hello I am writing a C# program which will require me running an executable in the same dir as the C# program. How do I do this? also can I pass arguments to the external program also?
thanks!
blah blah blah is here! blah blah » Close
1 answers
using System;
using System.Collections;
public class LaunchExe
{
public static void Main()
{
string strCmdLine;
strCmdLine ="/C notepad.exe myfile.txt";
System.Diagnostics.Process.Start("CMD.exe",strCmdLine);
}
}
The "/C" tells the program to launch the program and then terminate. Any arguments can be added one space after the executable name. In this case notepad will open and look for myfile.txt.
Regards,
MDTalley
answered 2 years ago by:
0
Yes. You can run (launch) any number of programs residing in the same directory as in which your C# program resides or anywhere else! (as long as you have the privilege to run).
You can pass parameters to external programs, too. But will your other program recognize those parameters? If yes, then you are in luck.
Here is a sample code that you can use to launch your external program.
System.Diagnostics.Process objProcess = System.Diagnostics.Process.Start(@"C:\otherprogram.exe", strParameters);
while (objProcess.HasExited == false)
System.Threading.Thread.Sleep(1000);
retVal = objProcess.ExitCode;
if (retVal == 0)
// You have executed the command okay
else
// the program returned with a code found in retVal.
objProcess = null;
Hope this answered your question.
answered 2 years ago by:
0
It has thanks to both of you!
also is it possible to make the window that the application running on minimized when it runs?
Thanks!
answered 2 years ago by:
0
Instead of passing the filename and arguments, try passing a ProcessStartInfo object.
System.Diagnostics.ProcessStartInfo objPSI = new System.Diagnostics.ProcessStartInfo();
objPSI.Arguments = "arg1 arg2 arg3 etc.,";
objPSI.FileName = @"C:\YourProgram.exe";
objPSI.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized; // or Hidden, Maximized or Normal
pass this object to the Process like so,
System.Diagnostics.Process objProcess = System.Diagnostics.Process.Start(objPSI);
while (objProcess.HasExited == false)
System.Threading.Thread.Sleep(1000);
int retVal = objProcess.ExitCode;
..........
answered 2 years ago by:
0
Once again thanks!
answered 2 years ago by:
0
This post was imported from csharpfriends, if you have a similiar question please ask it again.
All previous members have been migrated, hope you enjoy the new platform!