我有以下情况。我需要使用 Process() 在 c# 中运行一些 bat 文件,并且需要将它的输出结束错误重定向到文件。我知道该怎么做,如果我的进程不使用超时,我的代码可以正常工作。所以程序一直工作到进程结束(我期望的是进程在超时后被杀死)。以下是我的代码。
Process TcRunner = new Process();
TcRunner.StartInfo.FileName = "cmd.bat";
TcRunner.EnableRaisingEvents = true;
TcRunner.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
TcRunner.StartInfo.UseShellExecute = false;
TcRunner.StartInfo.RedirectStandardOutput = true;
TcRunner.StartInfo.RedirectStandardError = true;
try
{
TcRunner.Start();
}
catch (Exception ex)
{
Program.Print("Error: Could not start process\n");
return 1;
}
string run_log_out = TcRunner.StandardOutput.ReadToEnd();
string run_log_err = TcRunner.StandardError.ReadToEnd();
if (!TcRunner.WaitForExit(5000)
{
//kill the process
}
try
{
StreamWriter run_out = new StreamWriter(@".\out");
StreamWriter run_err = new StreamWriter(@".\err");
run_out.WriteLine(run_log_out);
run_err.WriteLine(run_log_err);
run_out.Close();
run_err.Close();
}
catch (Exception e)
{
Console.WriteLine("Cannot open out/err for writing");
}