WPF 列表框不添加整个字符串

WPF Listbox doesn't add whole string

我正在尝试通过 CMD 命令将多个用户添加到活动目录组并显示结果。 我想让用户在一个列表框中列出,在它旁边的另一个列表框中列出结果,不管它是否有效。

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();
}

当我使用 Console.WriteLine(strOutput); 打印结果时,我得到了这 3 行。

System error 5 occurred.

Access denied

The request is processed on a ****

但是当我尝试使用 LBresponse.Items.Add(strOutput); 将字符串添加到列表框时,只添加了最后一行。 前两个比最后一个重要一点。

我认为问题出在循环上,但我不知道如何解决它。

问题在于

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

必须替换为

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

要取出前两行。

ReadLine 将打印出第一行。