我正在使用Process
类来启动某个进程,它正在计算一些数据给它的控制台输出,我需要读取进程控制台的最后一行。应该怎么做?这是process.BeginOutputReadLine();
,但我不知道如何使用只读LAST行。如何读取输出流程类的最后一行?
1
A
回答
3
string lastLine = null;
while (!process.StandardOutput.EndOfStream)
{
lastLine = process.StandardOutput.ReadLine();
}
//do what you want here with lastLine;
1
这是应该做的代码,你需要的东西:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication
{
class Program
{
public static Process ShellStart(string aCmd, TextWriter aOutputWriter = null, TextWriter aErrorWriter = null)
{
var vProcess = new Process();
var vStartInfo = vProcess.StartInfo;
vStartInfo.FileName = Path.Combine(Environment.SystemDirectory, "CMD.EXE") ;
var vCmd = "/Q /C ";
vStartInfo.Arguments = vCmd + "\"" + aCmd + "\"";
vStartInfo.UseShellExecute = false;
vStartInfo.CreateNoWindow = true;
if (aOutputWriter != null)
{
vProcess.OutputDataReceived += (p, a) =>
{
if (a.Data != null)
{
aOutputWriter.WriteLine(a.Data);
}
};
vStartInfo.RedirectStandardOutput = true;
vStartInfo.RedirectStandardInput = true;
}
if (aErrorWriter != null)
{
vProcess.ErrorDataReceived += (p, a) =>
{
if (a.Data != null)
{
aErrorWriter.WriteLine(a.Data);
}
};
vStartInfo.RedirectStandardError = true;
vStartInfo.RedirectStandardInput = true;
}
if (!vProcess.Start()) return null;
if (aOutputWriter != null || aErrorWriter != null)
vProcess.Exited += (s, e) =>
{
if (aOutputWriter != null) aOutputWriter.Flush();
if (aErrorWriter != null) aErrorWriter.Flush();
};
if (aOutputWriter != null) vProcess.BeginOutputReadLine();
if (aErrorWriter != null) vProcess.BeginErrorReadLine();
if (vStartInfo.RedirectStandardInput) vProcess.StandardInput.Close();
return vProcess;
}
public static int ShellExec(string aCmd, TextWriter aOutputWriter = null, TextWriter aErrorWriter = null)
{
var vResult = -1;
using (var vProcess = ShellStart(aCmd, aOutputWriter, aErrorWriter))
if (vProcess != null)
{
vProcess.WaitForExit();
vResult = vProcess.ExitCode;
vProcess.Close();
}
return vResult;
}
public static IEnumerable<String> SplitLines(string s)
{
string vLine;
if (!String.IsNullOrEmpty(s))
using (var vReader = new StringReader(s))
while ((vLine = vReader.ReadLine()) != null)
{
yield return vLine;
}
}
public static string ShellExecGetLastLine(string aCmd)
{
var vOutput = new StringBuilder();
using (TextWriter vWriter = new StringWriter(vOutput))
{
ShellExec(aCmd, vWriter, null);
return SplitLines(Convert.ToString(vOutput).Trim()).LastOrDefault();
}
}
static void Main(string[] args)
{
Console.WriteLine(ShellExecGetLastLine("attrib"));
}
}
}
您可以使用ShellExecGetLastLine(command)
其中command
是路径到您的可执行文件(与参数一起,如果需要的话),以获得最后输出的一行。
在上面的例子中,它调用attrib
输出在当前目录下的所有文件的属性,并返回输出
相关问题
- 1. 如何获取子进程stderr流输出的最后N行?
- 2. 无缓冲的子流程输出(最后一行缺失)
- 3. 读取bash命令输出的最后一行
- 4. Java/Android - 从输入流读取后写入输出流
- 5. 运行批处理文件并在暂停后读取最后一行输出
- 6. 如何定期保存程序输出的最后一行?
- 7. 在张量流中获取dynamic_rnn的最后一个输出吗?
- 8. 文件中的最后读取行输出为空
- 9. 如何使用Estimator类输出张量流中的最后一层?
- 10. 从另一个进程的输出流中读取
- 11. 等待子流程的最后一行
- 12. 如何读取DocumenListener中的最后一个字或最后一行
- 13. 读取文件的最后一行
- 14. 如何读取python中最后几行?
- 15. 阅读标准输出的最后一行
- 16. 读取netstat输出的第一行?
- 17. 如何再次读取最后一次输入到Scanner实例?
- 18. 我如何提取每个输出的最后一个值?
- 19. 如何高效地读取C++文件的最后一行
- 20. 如何不读取文件中的最后一行?
- 21. fgets()PHP读取最后一行?
- 22. Fortran read()读取最后一行两次?
- 23. 从文件中读取最后一行
- 24. 如何在Linux中读取另一个进程的输出C
- 25. android LogCat只输出最后一行
- 26. 获取SSAS立方体上最后一个进程的输出
- 27. 如何获取textarea的最后一行?
- 28. 如何对流程的输出进行流式处理?
- 29. 从流式读取器中删除最后的x行
- 30. 如何读取http输入流
只能输出或两个输出和错误的最后一行? – dotctor
只输出,会尝试你的答案,看起来像这样 –