Monday, May 19, 2008

File Read & write using StreamReader

Code In-Line:

using System.IO;
using System.Text;

FileInfo fi = new FileInfo(@"D:\feed.txt");
DirectoryInfo dir = new DirectoryInfo(@"D:\");

protected void btnRead_Click(object sender, EventArgs e)
{
StreamReader sr = fi.OpenText();
string input = null;
while((input=sr.ReadLine())!=null)
{
Response.Write(input);
}
sr.Close();
}

protected void btnWrite_Click(object sender, EventArgs e)
{
StreamWriter wr = new StreamWriter(fi.FullName,true);
try
{
StringBuilder str = new StringBuilder();
str.Append(txtName.Text);
str.Append(txtPassword.Text);
str.Append(txtEmail.Text);
wr.WriteLine(str.ToString());
wr.Close();
Response.Write(str);
}
catch (Exception ex)
{
Response.Write(ex);
}
}

No comments: