C# 在关闭批处理文件之前,不会读取批处理文件的最后一行
我有一个执行bat文件并在我的c#应用程序控制台中输出bat文件输出的代码。但是当我运行应用程序时,直到bat文件关闭,bat文件的最后一个文件才被读取 我做了一些研究,有一些不同的方法将其输出到c#控制台,但它们都有相同的问题C# 在关闭批处理文件之前,不会读取批处理文件的最后一行,c#,process,C#,Process,我有一个执行bat文件并在我的c#应用程序控制台中输出bat文件输出的代码。但是当我运行应用程序时,直到bat文件关闭,bat文件的最后一个文件才被读取 我做了一些研究,有一些不同的方法将其输出到c#控制台,但它们都有相同的问题 private void button2_Click(object sender, EventArgs e) { if (Directory.GetFiles(string.Format(@"C:\Users\{0
private void button2_Click(object sender, EventArgs e)
{
if (Directory.GetFiles(string.Format(@"C:\Users\{0}\Downloads", Environment.UserName), "*", SearchOption.AllDirectories).Length <= 2)
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
//p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = string.Format(@"C:\Users\{0}\Downloads\test.bat", Environment.UserName);
p.OutputDataReceived += CaptureOutput;
p.Start();
p.BeginOutputReadLine();
//while (!p.StandardOutput.EndOfStream)
//{
// string line = p.StandardOutput.ReadLine();
// Console.WriteLine(line);
// //Console.WriteLine("--kl--");
//}
}
}
static void CaptureOutput(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}
控制台会打印
Waiting for 10 seconds, press a key to continue ... 9 8 7 6 5 4 3 2 1 0
YouCanCloseNow
但不打印
Press any key to continue . . .
直到cmd窗口关闭
所以正如我所说,我希望
按任意键继续代码>在控制台中打印,无需关闭cmd文件如何执行程序?F5或Ctrl+F5?您有刷新问题。您可以使用/k选项运行bat文件,该选项在bat终止后保持窗口打开。从cmd.exe中,尝试“帮助”以查看更多信息。@LasseVågsætherKarlsen我使用visual studio,只需点击“开始”Button@jdweng嗨,我忘了提到第一行在10秒后出现,这意味着只有在第二行应该被输出后,它才会在控制台中输出,如果你明白我的意思,我会做的。这就是为什么我建议使用/K选项运行bat文件。这样,bat窗口将保持打开状态,直到流程类终止时关闭它。您可能必须通过获取进程id来终止窗口。
Press any key to continue . . .