The method code for executing the batch file of *.bat in C

  • 2020-05-09 19:09:55
  • OfStack


static void Main(string[] args)
{
    Process proc = null;
    try
    {                
        string targetDir = string.Format(@"D:\adapters\setup");//this is where mybatch.bat lies
        proc = new Process();
        proc.StartInfo.WorkingDirectory = targetDir;
        proc.StartInfo.FileName = "mybatch.bat";
        proc.StartInfo.Arguments = string.Format("10");//this is argument
        proc.StartInfo.CreateNoWindow = false;
        proc.Start();
        proc.WaitForExit();
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception Occurred :{0},{1}", ex.Message,ex.StackTrace.ToString());
    }
}

If you want to hide the dos window at run time, use the following code


proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;


Related articles: