用C#应用中执行一个控制台命令 - code snippet
时间:2010-11-03 来源:congrats
下面是一个很实用的程序片段,可以通过这个函数启动执行一个控制台的命令。
/// <summary>
/// Runs a command with supplied araguments.
/// </summary>
/// <param name="command"></param>
/// <param name="arguments"></param>
private void runShellCommand(String command, String arguments)
{
Process process = new Process();
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.FileName = command;
process.StartInfo.Arguments = arguments;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
process.ErrorDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
while (process.HasExited == false)
{
Thread.Sleep(1000);
if (ApplicationStatus.IsAborting == true)
{
process.Kill();
break;
}
}
}
相关阅读 更多 +










