zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C#中执行批处理文件(*.bat)的方法代码

2023-06-13 09:14:46 时间

复制代码代码如下:


staticvoidMain(string[]args)
{
   Processproc=null;
   try
   {               
       stringtargetDir=string.Format(@"D:\adapters\setup");//thisiswheremybatch.batlies
       proc=newProcess();
       proc.StartInfo.WorkingDirectory=targetDir;
       proc.StartInfo.FileName="mybatch.bat";
       proc.StartInfo.Arguments=string.Format("10");//thisisargument
       proc.StartInfo.CreateNoWindow=false;
       proc.Start();
       proc.WaitForExit();
   }
   catch(Exceptionex)
   {
       Console.WriteLine("ExceptionOccurred:{0},{1}",ex.Message,ex.StackTrace.ToString());
   }
}

如果要运行时隐藏dos窗口,需使用下面的代码

复制代码代码如下:


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