Hi Guys,
I am reading a text file and i want to store that file ina string, below is my code, can anyone tell me what am i doing wrong

private void toolStripButton1_Click(object sender, EventArgs e)
        {
           // OpenFileDialog dialog = new OpenFileDialog();
            FolderBrowserDialog dialog1 = new FolderBrowserDialog();
            parser = new GrammarParser();
            parser.showStep += new GrammarParser.ShowStep(parser_showStep);
            if (dialog1.ShowDialog() == DialogResult.OK)
            {
                string nameD = dialog1.SelectedPath;
              
                string[] names = Directory.GetFiles(nameD, "*txt");
                for (int i = 0; i < names.Length; i++)
                {
                    
                    TextReader txtread = new StreamReader(names[i]);
                   MessageBox.Show(txtread.ReadToEnd());
                    // read file to the string


                   string text = txtread.ReadToEnd();
    
                    parser.Text = text;
                    
                   resultString = parser.SerParserString;
                   
                    
                    //   go to to database

                }
            }
        }

When you are reading a file, the API maintains a file pointer which points to where you are in the file. You can think of this as the position of the cursor. When you call ReadToEnd() , the data is read from the current position to the end of the file.

In your code, you call ReadToEnd() twice. After the first call, the cursor position is at the end of the file. So, when you call it second time, it reads from current position to the end. But since it is at the end already, it gives you an empty string. Hence, you either need to reset the file pointer between calls or (which should be more efficient) read it into a string, and then use that string to display the MessageBox and assign it to the parser object.

Here is the updated code:

private void toolStripButton1_Click(object sender, EventArgs e)
{
    // OpenFileDialog dialog = new OpenFileDialog();
    FolderBrowserDialog dialog1 = new FolderBrowserDialog();
    parser = new GrammarParser();
    parser.showStep += new GrammarParser.ShowStep(parser_showStep);
    if (dialog1.ShowDialog() == DialogResult.OK)
    {
        string nameD = dialog1.SelectedPath;
        string[] names = Directory.GetFiles(nameD, "*txt");
        for (int i = 0; i < names.Length; i++)
        {
            TextReader txtread = new StreamReader(names[i]);
            // read file to the string
            string text = txtread.ReadToEnd();
            txtread.Close(); //be good, close streams
            MessageBox.Show(text);
            parser.Text = text;
            resultString = parser.SerParserString;
        }
    }
}

Hope this helps.

if (dialog1.ShowDialog() == DialogResult.OK)
{
string nameD = dialog1.SelectedPath;

resultString =  File.ReadAllText(nameD);

}

how abt this?

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.