如何从输出流程类中读取最后一行?

时间:2015-06-14 14:21:57

标签: c# .net process

我正在使用Process类来启动某个进程,它正在计算一些数据,在它的控制台上提供输出,我需要读取最后一行进程控制台。怎么做?这是process.BeginOutputReadLine();的内容,但我不知道如何使用只读最后一行。

2 个答案:

答案 0 :(得分:4)

string lastLine = null;
while (!process.StandardOutput.EndOfStream) 
{
    lastLine = process.StandardOutput.ReadLine();
}

//do what you want here with lastLine;

答案 1 :(得分: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输出当前目录中所有文件的属性,并返回最后一行输出