1

I'm writing a c# application for validating the detailed information about no.of lines changes in SVN commit. 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}

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

I have to achieve the same in C# also. 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. I have tried the StandardOutput.ReadToEnd() but couldn't meet my requirement. Any detailed explanation will be helpful.

ManoRoshan
  • 109
  • 1
  • 1
  • 6

2 Answers2

1

Have you tried just running the command from a command prompt with C# as explained in this question?

string strCmdText = @"/C svn info -r 113653 ""F:\SVN""";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
Community
  • 1
  • 1
Lews Therin
  • 3,519
  • 2
  • 22
  • 50
  • Yes, I have tried. the command prompt opened but nothing was performed. But it is working fine while giving the argument directly in the command prompt. – ManoRoshan May 11 '16 at 13:37
  • Whoops, forgot the `/C` in the command text. That actually runs the command. See my edit. – Lews Therin May 11 '16 at 13:45
  • When I use the /c in the command as per your above example, multiple cmd prompt windows is opened and nothing is performed. – ManoRoshan May 12 '16 at 04:33
0

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.

Bassie
  • 7,390
  • 5
  • 36
  • 97