Share via


How to execute a batch file in C#

We can use the System.Diagnostics namespace to execute a Batch File in C#. Below is a simple C# sample.

using System;
using System.Diagnostics;

namespace InvokeBatchFile
{
  class Program
  {
    static void Main(string[] args)
    {
      ExecuteCmd("D://Hello.bat");
    }

    static void ExecuteCmd(string command)
    {
      var processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
      processInfo.CreateNoWindow = true;
      processInfo.UseShellExecute = false;
      processInfo.RedirectStandardError = true;
      processInfo.RedirectStandardOutput = true;

      var process = Process.Start(processInfo);

      process.OutputDataReceived += (object sender, DataReceivedEventArgs e) => Console.WriteLine("output>>" + e.Data);
      process.BeginOutputReadLine();

      process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) => Console.WriteLine("error>>" + e.Data);
      process.BeginErrorReadLine();

      process.WaitForExit();

      Console.WriteLine("ExitCode: {0}", process.ExitCode);

      process.Close();
    }
  }
}