110,537
社区成员
发帖
与我相关
我的任务
分享
public static string executeCmd(string command)
{
string strRst = "0";
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.AutoFlush = true;
p.StandardInput.WriteLine("@echo off");
p.StandardInput.WriteLine(command);
p.StandardInput.WriteLine("exit");
System.Timers.Timer timer = new System.Timers.Timer(3000);
timer.Start();
timer.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e)
{
p.CloseMainWindow();
timer.Enabled = false;
timer.Close();
};
strRst = p.StandardOutput.ReadToEnd();
timer.Enabled = false;
timer.Close();
strRst = strRst.Substring(strRst.IndexOf(command) + command.Length);
strRst = strRst.Substring(0,strRst.Length - 8);
if (p.StandardError.ReadToEnd() != "")
{
strRst = p.StandardError.ReadToEnd();
}
p.WaitForExit();
p.Close();
return strRst;
}