Executing Batch File in C#

前端 未结 12 1798
有刺的猬
有刺的猬 2020-11-22 05:22

I\'m trying to execute a batch file in C#, but I\'m not getting any luck doing it.

I\'ve found multiple examples on the Internet doing it, but it is not working for

12条回答
  •  攒了一身酷
    2020-11-22 05:49

    using System.Diagnostics;
    
    private void ExecuteBatFile()
    {
        Process proc = null;
        try
        {
            string targetDir = string.Format(@"D:\mydir");   //this is where mybatch.bat lies
            proc = new Process();
            proc.StartInfo.WorkingDirectory = targetDir;
            proc.StartInfo.FileName = "lorenzo.bat";
            proc.StartInfo.Arguments = string.Format("10");  //this is argument
            proc.StartInfo.CreateNoWindow = false;
            proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;  //this is for hiding the cmd window...so execution will happen in back ground.
            proc.Start();
            proc.WaitForExit();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
        }
    }
    

提交回复
热议问题