0

Good day everyone!

I'm currently working on a project and i need to open a text file and read a specific word.

As of now, i already know how to open a text file but my problem is i don't know how to read a specific word in a text file. (I know how to read a line but not a specific word)

I have three textbox and two buttons. the first textbox displays the content of the text file that i opened.

what i want to do is when i type a specific word in the second textbox it will appear on the third textbox if the word exists in the text file that i opened but if not a message box will pop out with a message "The word that you typed is not in the textfile".

Please help me... I'm new in c sharp language and i'm new in making projects at asp.net 3.5 windows application...

My project is called Data Capturing..

Thanks for the help

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by sundarchum
0

Good day everyone!

I'm currently working on a project and i need to open a text file and read a specific word.

As of now, i already know how to open a text file but my problem is i don't know how to read a specific word in a text file. (I know how to read a line but not a specific word)

I have three textbox and two buttons. the first textbox displays the content of the text file that i opened.

what i want to do is when i type a specific word in the second textbox it will appear on the third textbox if the word exists in the text file that i opened but if not a message box will pop out with a message "The word that you typed is not in the textfile".

Please help me... I'm new in c sharp language and i'm new in making projects at asp.net 3.5 windows application...

My project is called Data Capturing..

Thanks for the help

Hi ilaref,

Try this. Hope it helps you.

public partial class CheckFileForWord : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            txtBox3.Text = "";
            string sourceFile = @"Source.txt";

            // Read the lines from the file.
            using (StreamReader reader = new StreamReader(sourceFile))
            {
                // Write all the text contained in the text file to the first text box.
                txtBox1.Text = reader.ReadToEnd();             
            }
        }

        protected void btnCheck_Click(object sender, EventArgs e)
        {
            string sourceFile = @"Source.txt";
            string input = null;
            // Fetch the text contained in the second text box the word which has to be found.
            string word = txtBox2.Text;
            bool isWordFound = false;

            txtBox3.Text = "";

            // Read the appropriate line from the file.
            using (StreamReader reader = new StreamReader(sourceFile))
            {
                while ((input = reader.ReadLine()) != null)
                {
                    if (input.Contains(word))
                    {
                        txtBox3.Text = "Word found";
                        isWordFound = true;
                        return;
                    }
                }

                if (!isWordFound)
                {
                    txtBox3.Text = "Word not found";
                }                
            }
        }
    }

Mark as "solved" if this helps to solve your requirement. Thanks.

Edited by mike_2000_17: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.