Run batch file

  • Thread starter Thread starter Arjen
  • Start date Start date
Hi,
I have a batch file "MyFile.bat", and I need to run it from a C#
application.
How do I do it ?

Yoav.
 
Hi Yoavo,
here is how you would do that with a couple of the possible options you
might want to set:

using System.Diagnostics;

ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();

//specify the nam andthe arguements you want to pass to the command prompt
psi.FileName = @"c:\test123.bat";
psi.Arguments = "1 2 3 4";

//if you don't want a console window popping up then set this property
pi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

//Create new process and set the starting information
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = psi;


//Set this so that you can tell when the process has completed
p.EnableRaisingEvents = true;


p.Start();

//wait until the process has completed
while(!p.HasExited)
{
System.Threading.Thread.Sleep(1000);
}

//check to see what the exit code was
if(p.ExitCode != 0)
{
//some error occurred
}


Hope that helps
Mark R Dawson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top