Friday 10 February 2012

FILE HANDLING IN ASP.NET : RETRIEVE DATA FROM A FILE



Protected Void RetriveFromFile()
{
position = 0;
total = 0;

FileStream fs = new FileStream(Server.MapPath("Temp/temp.txt"), FileMode.Open, FileAccess.Read);

StreamReader sr = new StreamReader(fs);
while (!sr.EndOfStream)
{
for (int i = 0; i < 5; i++)
{
sr.ReadLine();
}
total++;
}
position++;
read_text(position);

sr.Close();
fs.Close();
}
protected void read_text(long pos)
{
FileStream fs = new FileStream(Server.MapPath("temp.txt"), FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);

if (pos == 1)
{
sr.BaseStream.Seek(0, SeekOrigin.Begin);

TextBox1.Text = sr.ReadLine();
TextBox2.Text = sr.ReadLine();
TextBox3.Text = sr.ReadLine();
TextBox4.Text = sr.ReadLine();

sr.ReadLine();
            }
 else
{
for (int i = 0; i < (pos - 1) * 5; i++)
{
sr.ReadLine();
}
TextBox1.Text = sr.ReadLine();
TextBox2.Text = sr.ReadLine();
TextBox3.Text = sr.ReadLine();
TextBox4.Text = sr.ReadLine();

sr.ReadLine();
}

sr.Close();
fs.Close();
}

========================================================================

No comments:

Post a Comment