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.

Charles He-MSFT 96 Reputation points Microsoft Employee
2020-03-27T08:50:46.397+00:00

This is a MSDN question asked by spum0n1, the source is 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..

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


      }
}
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,676 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Alex Li-MSFT 1,096 Reputation points
    2020-03-27T09:44:44.907+00:00

    Hi,

    Welcome to our Microsoft Q&A platform!

    Do you want to get all files?You can try the following code:

     string path = @"xxxxxx";
                DirectoryInfo root = new DirectoryInfo(path);
                FileInfo[] files = root.GetFiles();
                foreach (var item in files)
                {
                    var fileStream = new FileStream(item.FullName, FileMode.Open);
                    using (StreamReader reader = new StreamReader(fileStream))
                    {
    
                    }
                }
    

    Thanks.