c# 使用ProcessStartInfo可以捕捉到exe的控制台输出。
如果这个exe程序用了一个bat文件打开,就无法捕捉输出了,此时应该用什么方法??
using System;
using System.Diagnostics;
class Program
{
static void Main()
{
Process process = new Process();
process.StartInfo.FileName = "path/to/batch/file.bat";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.OutputDataReceived += Process_OutputDataReceived;
process.ErrorDataReceived += Process_ErrorDataReceived;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
process.Close();
}
static void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
{
Console.WriteLine("Output: " + e.Data);
}
}
static void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
{
Console.WriteLine("Error: " + e.Data);
}
}
}
哥们,这有一个demo ,看看对你有没有帮助