类
public static void ExecuteCmd(List<string> cmds)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
p.StartInfo.CreateNoWindow = false;//不显示程序窗口
p.Start();//启动程序
for (int i = 0; i < cmds.Count; i++)
{
p.StandardInput.WriteLine(cmds[i]);
}
p.StandardInput.AutoFlush = true;
//向标准输入写入要执行的命令。这里使用&是批处理命令的符号,表示前面一个命令不管是否执行成功都执行后面(exit)命令,如果不执行exit命令,后面调用ReadToEnd()方法会假死
//同类的符号还有&&和||前者表示必须前一个命令执行成功才会执行后面的命令,后者表示必须前一个命令执行失败才会执行后面的命令
p.StandardInput.WriteLine("exit");
//获取cmd窗口的输出信息
string output = p.StandardOutput.ReadToEnd();
p.Close();
}
调用
//新建一个cmds列表,用于存储命令
List<string> cmds = new List<string>();
//将每一行命令Add进list
cmds.Add(@"cd /d " + srcCodedir);
cmds.Add("\"" + appBaseDir + "vcvars32.bat" + "\"");
cmds.Add("csc /r:" + xmlfilename + "clr.dll;GeoModelling.Packing_DotNet.dll;ngismdl.dll;ngisudx.dll;nxmodel.context.csharp.dll;nxdat.csharp.dll Program.cs /platform:x86");
//执行cmd命令
ExecuteCmd(cmds);
来源:CSDN
作者:DxCaesar
链接:https://blog.csdn.net/DxCaesar/article/details/104308712