C#运行DOS命令
1 Process p = new Process(); 2 ProcessStartInfo pi = new ProcessStartInfo(); 3 pi.FileName = "cmd.exe"; 4 pi.UseShellExecute = false; 5 pi.RedirectStandardInput = true; 6 pi.RedirectStandardOutput = true; 7 pi.RedirectStandardError = false; 8 pi.CreateNoWindow = true; 9 p.StartInfo = pi; 10 string strOutput = null; 11 try 12 { 13 p.Start(); 14 p.StandardInput.WriteLine(cmd); //写入要执行的命令 可以多条写入也可以复合命令写入 15 p.StandardInput.WriteLine("D:"); 16 p.StandardInput.WriteLine("npm run build"); 17 p.StandardInput.WriteLine("exit"); 18 strOutput = p.StandardOutput.ReadToEnd(); 19 p.WaitForExit(); 20 p.Close(); 21 } 22 catch (Exception e) 23 { 24 //异常处理 25 }