locked
I want to be able to select a folder and search/print to a list the key phrase I've specified. Currently I can only do individual files. I'm a beginner. RRS feed

  • Question

  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Windows.Forms;
    using System.IO;
    using System.Speech.Synthesis;
    using Microsoft.Office.Interop.Excel;
    
    namespace WpfApp2
    {
          /// <summary>
          /// Interaction logic for MainWindow.xaml
          /// </summary> 
          public partial class MainWindow : System.Windows.Window
          {
                public MainWindow()
                {
                      InitializeComponent();
                      errors.ItemsSource = list;
                      
                }
    
                List<string> list = new List<string>();
    
                int errorCount = 0;
                private void Button_Click_1(object sender, RoutedEventArgs e)
    
                {
    
    
    
                      var fileContent = string.Empty;
                      var filePath = string.Empty;
                     
                      using (OpenFileDialog openFileDialog = new OpenFileDialog())
                      {
                            openFileDialog.InitialDirectory = "c:\\";
                            openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                            openFileDialog.FilterIndex = 2;
                            openFileDialog.RestoreDirectory = true;
                            openFileDialog.Multiselect = true;
    
                            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                            {
                                  //Get the path of specified file
                                  filePath = openFileDialog.FileName;
    
                                  //Read the contents of the file into a stream
                                  var fileStream = openFileDialog.OpenFile();
    
                                  list.Clear();
                                  errors.Items.Refresh();
                                  
    
                                  using (StreamReader reader = new StreamReader(fileStream))
                                  {
                                        while (!reader.EndOfStream)
                                        {
                                              //check each line
    
                                              var line = reader.ReadLine();
                                              // check to see if the line is an error.
                                              if (line.StartsWith("<div class='CriticalError"))
                                              {
                                                    
                                                    list.Add(line);
                                                    errors.Items.Refresh();
                                                    errorCount++;
    
                                              }
    
                                        }
    
                                  }
                                  if (errorCount >= 1) {
                                        SpeechSynthesizer ss = new SpeechSynthesizer();
                                        ss.Speak("The error log has been collated.");
                                  }
                                  
                                  if (errorCount == 0)
                                  {
                                        SpeechSynthesizer ss = new SpeechSynthesizer();
                                        ss.Speak("No errors found");
                                        
                                  }
                            }
                      }
    
    
                }
    
                private void errors_SelectionChanged(object sender, SelectionChangedEventArgs e)
                {
                     
                      
    
                }
    
                private void Button_Click(object sender, RoutedEventArgs e)
                {
                      var stringBuilder = new StringBuilder();
                      foreach (var item in list)
                      {
                            stringBuilder.AppendLine(item);
                      }
                      System.Windows.Clipboard.SetText(stringBuilder.ToString());
                }
    
                
          }
    }


    • Moved by KareninstructorMVP Thursday, March 26, 2020 4:59 PM moved from C# forum
    • Edited by spum0n1 Thursday, March 26, 2020 5:29 PM
    Thursday, March 26, 2020 4:47 PM

All replies

  • Hello,

    Please indicate what the object type for errors is. Since this is WPF your post is being moved to the WPF forum for better visibility.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, March 26, 2020 4:59 PM
  • Hi,

    Since MSDN WPF forum will be read only from 3/30/2020, we have created a new thread on Microsoft Q&A I want to be able to select a folder and search/print to a list the key phrase I've specified. Currently I can only do individual files. I'm a beginner., please follow this thread for further discussion about your question.

    Best Regards,
    Charles


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 27, 2020 8:51 AM