question

CharlesHe-MSFT avatar image
0 Votes"
CharlesHe-MSFT asked ·

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.

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());
             }
 
             
       }
 }
wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AlexLi-MSFT avatar image
0 Votes"
AlexLi-MSFT answered ·

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.

· 1 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, Thanks for responding. Where in my code would that section go please? is it to replace something I've already got now, or does it go in a new method? Thanks.

0 Votes 0 · ·