public class Console : IRun
{
public Console(){
this.TimeOut = ;
}
public string Result
{
get;
set;
}
public string Error
{
get;
set;
}
public int TimeOut
{
get;
set;
}
public string[] Cmds
{
get;
set;
}
#region IRun 成员
public void Run()
{
using (System.Diagnostics.Process process = new System.Diagnostics.Process())
{
process.StartInfo = new System.Diagnostics.ProcessStartInfo("cmd")
{
RedirectStandardOutput = true,
RedirectStandardInput = true,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardError = true
};
process.Start();
System.Text.StringBuilder cmd = new StringBuilder();
foreach (string arg in Cmds)
{
if (string.IsNullOrEmpty(arg))
{
continue;
}
process.StandardInput.WriteLine(arg);
cmd.Append(arg);
}
process.StandardInput.WriteLine(@"exit");
try
{
process.WaitForExit(Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["CMDTimeOut"]));
}
catch
{
process.WaitForExit();
}
this.Result = process.StandardOutput.ReadToEnd();
this.Error = process.StandardError.ReadToEnd();
process.Close();
if (!string.IsNullOrEmpty(this.Error))
{
throw new Exception(string.Format("出错命令:\r\n{0}\r\n{1}", cmd.ToString(), this.Error));
}
}
}
#endregion
}