简体   繁体   English

如何获取命令提示符输出

[英]How to get command prompt output

I'm writing ac# application for validating the detailed information about no.of lines changes in SVN commit.我正在编写 ac# 应用程序来验证有关 SVN 提交中行数更改的详细信息。 After providing the below arguments in command prompt, it displays the revision number, author name and last changed date etc...在命令提示符中提供以下参数后,它会显示修订号、作者姓名和上次更改日期等...

Argument: svn info –r {revision no} {Source path}参数: svn info -r {revision no} {Source path}

Eg - svn info -r 113653 "F:\\SVN"例如 - svn info -r 113653 "F:\\SVN"

I have to achieve the same in C# also.我也必须在 C# 中实现相同的目标。 While giving the above arguments in C#, it should read the output(revision number, author name and last changed date) from the command prompt and store it in a string.在 C# 中给出上述参数时,它应该从命令提示符读取输出(修订号、作者姓名和上次更改日期)并将其存储在一个字符串中。 I have tried the StandardOutput.ReadToEnd() but couldn't meet my requirement.我尝试过StandardOutput.ReadToEnd()但无法满足我的要求。 Any detailed explanation will be helpful.任何详细的解释都会有所帮助。

Have you tried just running the command from a command prompt with C# as explained in this question ?您是否尝试过使用 C# 从命令提示符运行命令,如本问题中所述

string strCmdText = @"/C svn info -r 113653 ""F:\SVN""";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);

You can use the following method to run a command and retrieve the standard output from the console :您可以使用以下方法运行命令并从控制台检索标准输出:

    public static string StdOut(string args)
    {
        string cmdOut = "";

        ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/C " + args)
        {
            WindowStyle = ProcessWindowStyle.Hidden,
            UseShellExecute = false,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            CreateNoWindow = true
        };

        cmdOut = ExecuteCommand(cmdOut, startInfo);
        return cmdOut;
    }

It will return the output as a string.它将以字符串形式返回输出。 You will also need this method (as it is used in the above):您还将需要此方法(因为它在上面使用过):

    private static string ExecuteCommand(string cmdOut, ProcessStartInfo startInfo)
    {
        Process p = Process.Start(startInfo);
        p.OutputDataReceived += (x, y) => cmdOut += y.Data;
        p.BeginOutputReadLine();
        p.BeginErrorReadLine();
        p.WaitForExit();

        return cmdOut;
    }

p.OutputdataReceived is a DataReceivedEventHandler and it will concatenate any std output received onto the cmdOut variable. p.OutputdataReceived 是一个DataReceivedEventHandler ,它将把接收到的任何 std 输出连接到 cmdOut 变量上。

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

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