Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to read a textfile with extention .txt and let is show into a textbox within a C# program.
The code I created only show me the path I entered but doesn't load in the text from the txtfile.

This is the code I got so far.

Any ideas on how to load in the text in the file instead

C#
private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string value = @"C:\Users\Respawn\Documents\AllTxtFiles.txt";
            StringReader OpenFile = new StringReader(value) ;
            textBox1.Text = OpenFile.ReadToEnd();
        }
Posted
Updated 15-Feb-13 2:18am
v2
Comments
Abhishek Pant 15-Feb-13 8:47am    

Need to use a Stream Reader

StreamReader OpenFile = new StreamReader(value);
textBox1.Text = OpenFile.ReadToEnd();
OpenFile.Close();
 
Share this answer
 
Hi,

As alternative to Solution 1, you can use File.ReadAllText[^] to read all text from a file:
C#
string s = File.ReadAllText(@"C:\Users\Respawn\Documents\AllTxtFiles.txt");
textBox1.Text = s;

Hope this helps.
 
Share this answer
 
v2
Comments
Chris Reynolds (UK) 15-Feb-13 8:29am    
Even nicer
Thomas Daniels 15-Feb-13 8:30am    
Thank you!
Abhishek Pant 15-Feb-13 8:50am    
good one.+5
Thomas Daniels 15-Feb-13 9:00am    
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900