可以执行多条命令,用“\r\n”分割
using System;
using System.Diagnostics;
namespace Tool
{
public class CMDHelper
{
public static string[] ExeCommand(string commandText)
{
// System.Text.Encoding.RegisterProvider (System.Text.CodePagesEncodingProvider.Instance);
// System.Console.OutputEncoding = System.Text.Encoding.GetEncoding("gb2312");
using (var p = new Process())
{
p.StartInfo.FileName = Environment.GetEnvironmentVariable("ComSpec");
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine(commandText);
p.StandardInput.WriteLine("exit");
p.StandardInput.Close();
var outputTask = p.StandardOutput.ReadToEndAsync();
strError = p.StandardError.ReadToEnd();
strOutput = outputTask.Result;
p.WaitForExit();
return new string[] { strOutput, strError };
}
}
}
}