»Dotnet Ads
»Message Boards
Message Boards
Dotnet Books
»Member Details
Register
Login
LogOut
Submit Code
Submit Jobs
Submit Projects
»Competition
Community
Winners
Prizes
Write For Us
Members
»Other Resources
Links
Dotnet Resources
|
Reading and Writing Text Files In this article i will show you how to read a text from a text file
1.open the file
2.read the file
3.close the file
The below code shows how to read from a text file. I have added the code inside a button click event. also dont forget to add the namespace Sytem.IO
using System.IO;
private void button2_Click(object sender, System.EventArgs e)
{
TextReader tr = new StreamReader("c:/t.txt");
string s = tr.ReadLine();
while(s != null)
{
s = tr.ReadLine();
this.textBox2.Text += s + "\n";
}
tr.Close();
}
|
The StreamReader class constructor to create an instance of a TextReader. The StreamReader class includes additional overloads that allow you to specify the file in different ways, text format encoding, and buffer info. This program opens the t.txt file
ReadLine() reads a line of text from the text file
after the file is read close the file
|