简体   繁体   English

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

[英]How to read last line from output process class?

I'm using Process class to start some process, it's calculating some data giving output at it's console and I need to read last line of process console. 我正在使用Process类来启动某个流程,它正在计算一些数据,以在其控制台上提供输出,并且我需要阅读流程控制台的最后一行。 How it should be done? 应该怎么做? It's something with process.BeginOutputReadLine(); process.BeginOutputReadLine(); but I do not know how to use for read only LAST line. 但我不知道如何用于只读LAST行。

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

//do what you want here with lastLine;

Here is the code that should do what you need: 这是应该执行您需要的代码:

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"));
        }
    }
}

You can use ShellExecGetLastLine(command) where command is the path to your executable (along with the arguments, if required) to get the last line of the output. 您可以使用ShellExecGetLastLine(command) ,其中command是可执行文件的路径(如果需要,还带有参数)以获取输出的最后一行。

In the example above, it calls attrib that outputs attributes of all files in the current directory, and returns the last line of output 在上面的示例中,它调用attrib输出当前目录中所有文件的属性,并返回输出的最后一行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM