private void MenuExportSelected_Click(object sender, RoutedEventArgs e) { var selectedItems = OutputListbox.SelectedItems; var saveFile = new SaveFileDialog(); saveFile.Filter = "Text file (*.txt)|*.txt"; saveFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); if (saveFile.ShowDialog() == true) { try { using (FileStream fs = File.Open(saveFile.FileName, FileMode.CreateNew)) { using (StreamWriter sw = new StreamWriter(fs)) { foreach (var item in selectedItems) sw.WriteLine(item.ToString()); } } } catch (Exception ex) { MessageBox.Show( ex.ToString(), "Failed to export", MessageBoxButton.OK, MessageBoxImage.Error); } OutputListbox.UnselectAll(); } }
private void AppendLine(string line) { if (OutputListbox.Items.Count > 10000) OutputListbox.Items.RemoveAt(0); OutputListbox.Items.Add(line); OutputListbox.SelectedIndex = OutputListbox.Items.Count - 1; OutputListbox.ScrollIntoView(OutputListbox.SelectedItem); OutputListbox.UnselectAll(); }