December 14, 2008

Using Process

// Starting a windows program with Process
private void StartXXX()
{
 string dir = @"C:\CODE\blahblahblah\Server\XXX\bin";
 string exe = "XXX.exe";

 Process shellRunProcess = new Process();
 shellRunProcess.StartInfo.FileName = exe;
 shellRunProcess.StartInfo.WorkingDirectory = dir;
 shellRunProcess.StartInfo.Arguments = "-c";
 shellRunProcess.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
 shellRunProcess.Start();
}


// Terminating a process
private void TerminateXXX()
{
 // Remove the '.exe' extension when looking for the process
 Process[] processes = Process.GetProcessesByName("XXX"); 
 foreach (Process p in processes)
 {
  p.CloseMainWindow();
 }
}
When running a batch file or console type application, you can start if like so and have the output redirected to a control in your form. See this link for more information Launching A Console Process And Redirecting the Output
private void StartProcessWithoutWindow()
{
 Process process = new Process();
 process.StartInfo.UseShellExecute = false;
 process.StartInfo.RedirectStandardOutput = true;
 process.StartInfo.RedirectStandardError = true;
 process.StartInfo.CreateNoWindow = true;
 process.StartInfo.FileName = FileName;
 process.StartInfo.Arguments = Arguments;
 process.StartInfo.WorkingDirectory = WorkingDirectory;
 process.Start();
}

No comments: