2

Using C#, I'm creating and starting a process in order to read and parse its response. The process is a bash command that produces as output a json.

This json is then parsed and used in other portions of code.

StandardOutput is redirected and I'm able to read only a portion of it if it is too large. In this cases I'm unable to retrieve the missing portion to cuncatanate the strings and get a proper json.

My code:

System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = true;
startInfo.FileName = "/bin/bash";
startInfo.Arguments = $"-c \"{i_Command.cmdLine}\"";
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardInput = false;
startInfo.UseShellExecute = false;
foreach (KeyValuePair<string, string> var in i_Command.envVariables)
{
    startInfo.EnvironmentVariables.Add(var.Key, var.Value);
}
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo = startInfo;
process.Start();
string result = "";
if (process.StandardOutput != null)
{ 
    result = process.StandardOutput.ReadToEnd(); 
}
process.WaitForExit();

The called bash command is an aws call, but I don't know if it is relevant to the issue, because with short outputs I'm not experiencing output truncation.

Let me know if you need more details, thanks in advance.

Benry893
  • 21
  • 4
  • Are you sure all of the output is available to read before you read it all? I would assume you should be calling `WaitForExit` and *then* reading the stdout. – DavidG Jun 29 '21 at 13:27
  • HI @DavidG, just tried and nothing changed. I think the issue may be caused by buffer size, but I'm not being able to deal with that limit. – Benry893 Jun 29 '21 at 13:32
  • Then you may need to keep calling `ReadToEnd`, appending it to a local string and exiting when `StandardOutput.EndOfStream` is true? – DavidG Jun 29 '21 at 13:40
  • @DavidG nothing changed. I also tried reading line by line and in other ways like the first ones mentioned here with no success: https://stackoverflow.com/questions/8808663/get-live-output-from-process . – Benry893 Jun 29 '21 at 13:53

0 Answers0