Hi all trying get small program work.

I want my program to search for a word within a text file then out put the word with the rest of the fileds with the sentence.

so say i input 4 word/numbers from 4 different text boxes.

the ouput to the text file would be: Michael,Hall,september,21

Then i want to search the file for a certain word say september

i want this program to output all the fields that have september in it like so:

Michael,Hall,september,21

i know how to search for the word in the text file just don't know how to get all fields with the contain the the word.

My code so far:

namespace FriendBirthdayReminder
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            searchFile(@"C:\Users\michael\Documents\data.txt", textBox1.Text);
        }
        private void searchFile(String file, String searchText)
        {
            System.IO.StreamReader reader = new System.IO.StreamReader(file);
            String text = reader.ReadToEnd();
            if (System.Text.RegularExpressions.Regex.IsMatch(text, searchText, RegexOptions.IgnoreCase))
            {
                richTextBox1.SelectedText = searchText;
            }
            else
            {
                MessageBox.Show("Sorry, but " + searchText + " could not be found in the given file", "No Results");
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

By the way the input for the text file (the four text boxes i was on about) is in a different program if you wonder this program jsut finds the word and output the sentence in a rich text box

Try processing the file line-by-line. You can use System.IO.StreamReader.ReadLine() method to read the next line.

StreamReader reader = new StreamReader(file);
string line = null;
while ((line = reader.ReadLine()) != null) {
    if (Regex.IsMatch(line, searchText, RegexOptions.IgnoreCase)) {
        //TODO: append line to the output here
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.