title: C# WPF help in either 1. removing first two entries in listbox or 2. preventing it from displaying the command syntax from batch in output
body: <p>Here is the code snippet I have that I am calling during window_loaded event in my wpf. It will execute a batch file, and output the results in listbox. However, the first entry within listbox is showing the command itself from within the batch file. I would like to remove the first entry, or if that can't be done in an efficient manner then to prevent it from displaying it in the first place.</p>
  
  <p>I have tried using item count remove, but it throws an outofargument exception. My educated guess would be that despite the fact that it is trying to add items in window_loaded, it does not finish adding it until the window has actually finished loading. As such when I call the syntax to remove item from index count, it throws that error since the listbox is still empty at this point. So how can I add data while window is loading, and once it is finished adding the data, to remove the first entry that includes syntax from batch or to prevent it from displaying the first entry.</p>
  
  <pre><code>private void Window_Loaded(object sender, RoutedEventArgs e)
  {
      var info = new ProcessStartInfo("extractServiceList.bat") { UseShellExecute = false, RedirectStandardOutput = true };
      var proc = new Process { StartInfo = info, EnableRaisingEvents = true };
  
      proc.OutputDataReceived += (obj, args) =&gt;
      {
          if (args.Data != null)
          {
              this.Dispatcher.Invoke(() =&gt;
              {
                  listBoxServices.Items.Add(args.Data);
  
                  //below logic for removing whitespaces from listbox view
                  int count = listBoxServices.Items.Count;
  
                  for (int i = count - 1; i &gt;= 0; i--)
                  {
                      //Condition evaluating to see if it return true for = listbox items empty and
                      //looping through each entry to remove the whitespace entry
                      if (String.IsNullOrWhiteSpace(listBoxServices.Items[i].ToString()))
                      {
                          listBoxServices.Items.RemoveAt(i);
                      }
                  }
                  //end of logic above for removing whitespaces from listbox view
              });
  
              //listBoxServices.Items.RemoveAt(listBoxServices.Items.Count - 1);
          }
      };
  
      proc.Start();
      proc.BeginOutputReadLine();
  }
  </code></pre>
  
  <p><a href="https://i.imgur.com/Sif8wky.png" rel="nofollow noreferrer">https://i.imgur.com/Sif8wky.png</a></p>