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

  • Thread starter Thread starter spum0n1
  • Start date Start date
S

spum0n1

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


}
}

Continue reading...
 
Back
Top