user6874415
user6874415

Reputation:

How to get the entire process.StandardOutput as a string?

I have the below code.

        Process compiler = new Process();
        compiler.StartInfo.FileName = "cmd.exe";
        compiler.StartInfo.Arguments = ("/C git push gitlab --delete branch");
        compiler.StartInfo.UseShellExecute = false;
        compiler.StartInfo.CreateNoWindow = false;
        compiler.StartInfo.RedirectStandardOutput = true;
        compiler.Start();
        string output = compiler.StandardOutput.ReadToEnd();
        compiler.WaitForExit();
        compiler.Close();

I get only the null value in string output.But I got the below data in output screen.

error: unable to delete 'branch': remote ref does not exist

error: failed to push some refs to 'https://gitlab.company.com/test2.git'

Why i got the null value in the output string? Why did the process compiler.StandardOutput.ReadToEnd(); failed to fetch those line?

I got the output data in the console screen because I set false in CreateNoWindow. Else I would not get the data in console screen.

Anyone suggest the way to get the output screen data from the Process.StandardOutPut.

Upvotes: 1

Views: 2453

Answers (2)

user6874415
user6874415

Reputation:

I get the desired output in process.Standarderror

Upvotes: 0

Gururaj
Gururaj

Reputation: 539

The reason you get a null value when you read StandardOutput is because the application has written nothing at that point in time.

There are two events associated with Process which you can subscribe to read the data - OutputDataReceived and Exited

OutputDataReceived Documentation & Example

Exited Documentation & Example

Upvotes: 1

Related Questions