Search code examples
c#wpfcmdlistboxlistboxitem

WPF Listbox doesn't add whole string


I am trying to add multiple User to an active directory group via CMD commands and display the result. I want to have the users listed in one listbox and next to it in another listbox the results, whether it worked or not.

for (int i = 0; i < amount; i++)
{
 System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
 string strCommand = "cmd.exe";
 string strCommandParameters = Parameters;
 pProcess.StartInfo.FileName = strCommand;
 pProcess.StartInfo.Arguments = strCommandParameters;
 pProcess.StartInfo.UseShellExecute = false;
 pProcess.StartInfo.CreateNoWindow = true;
 pProcess.StartInfo.RedirectStandardOutput = true;
 pProcess.Start();

 string strOutput = pProcess.StandardOutput.ReadToEnd();

 LBresponse.Items.Add(strOutput);
 Console.WriteLine(strOutput);

 pProcess.WaitForExit();
}

When I print the results with Console.WriteLine(strOutput);, I get these 3 lines out.

System error 5 occurred.

Access denied

The request is processed on a ****

But when I try to add the string to the listbox with LBresponse.Items.Add(strOutput); only the last line is added. The first two are a bit more important than the last one.

I think the problem is the loop, but I have no clue how to fix it.


Solution

  • Problem was that

    pProcess.StartInfo.RedirectStandardOutput = true;
    string strOutput = pProcess.StandardOutput.ReadToEnd();
    

    must be replaced by

    pProcess.StartInfo.RedirectStandardError = true;
    string strOutput = pProcess.StandardError.ReadToEnd();
    

    To get the first two lines out.

    ReadLine will print out the first line.