-1

I have this desktop application I am working on that should keep track of small comments of certain things. The idea is to have a savefiledialog for saving files and and openfiledialog for opening files. But in the openToolStripMenuItem_Click method I have trouble reading from a file and setting the textbox1.Text to the file being read to and opening the text in the file to be edited for small comments. Here is the code below

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
    OpenFileDialog fo = new OpenFileDialog();
    fo.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    fo.ShowDialog();
    if (File.Exists(fo.FileName)) 
    {
        using (StreamReader reader = new StreamReader(fo.FileName,Encoding.UTF8)) {
                textBox1.Text = reader.ReadToEnd();   
            }

    }
}

this works fine

    using (StreamReader reader = new StreamReader(fo.FileName,Encoding.UTF8)) {
                        textBox1.Text = reader.ReadToEnd();   
                    }

and so does this

    textbox1.Text = File.ReadAllText(fo.FileName);

EDIT: There was nothing in the file I was opening.

june1992
  • 151
  • 2
  • 10
  • You mention you have "trouble reading from file & setting the textbox1" What trouble exactly? – Kiong Aug 29 '15 at 00:55
  • 2
    When asking question that exactly matches MSDN article title like [How to: Read From a Text File C# ](https://msdn.microsoft.com/en-us/library/ezwyzy7b.aspx) consider linking to the article and clarify what exactly is not clear. – Alexei Levenkov Aug 29 '15 at 01:02
  • When I read from a file and set the value from textbox1.Text to a StreamReader object method or File.Open i get a System.IO.StreamReader or nothing at all, inside the textbox instead of the actual file contents. – june1992 Aug 29 '15 at 01:08
  • It's worth your while to learn how to use the return type of methods to your advantage. The return type of File.OpenText is a StreamReader - you could Google 'StreamReader' and find out how to use one. Hint: ToString() isn't useful. Look at the answers to this question, the MSDN link provided, or the question this is marked as a duplicate of. All will help you here. – Wai Ha Lee Aug 29 '15 at 01:08
  • 1
    ... You *can* use StreamReader, just not like that. – Wai Ha Lee Aug 29 '15 at 01:10
  • I tried several different ways . I just happened to post one of the things I tried – june1992 Aug 29 '15 at 01:11
  • Its still is not working... – june1992 Aug 29 '15 at 01:11
  • I don't think this is a duplicated to the other question marked. It isn't about reading file contents. Its about using the OpenFileDialog API to read the file contents. – Kiong Aug 29 '15 at 01:24

2 Answers2

1

Use System.IO.File.ReadAllText to read all of the contents of a file into a string.

John Koerner
  • 37,428
  • 8
  • 84
  • 134
1

File.OpenText will return you an StreamReader, that's a class for manipualting text streams, it's not the text content on the file.

If you want to read it then use File.ReadAllText(yourFIle), it will return just a string with the content of the file.

Gusman
  • 14,905
  • 2
  • 34
  • 50