Friday, 10 February 2012

FILE HANDLING IN ASP.NET : DELETE FROM FILE



Protected Void DeleteFromFile()
{
FileStream fr = new FileStream(Server.MapPath("temp.txt"), FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fr);
 FileStream fwt = new FileStream(Server.MapPath("abcd.txt"), FileMode.Create, FileAccess.Write);
 StreamWriter swt = new StreamWriter(fwt);
for (int i = 1; i <= total; i++)
{
if (position == i)
{
for (int j = 0; j < 5; j++) sr.ReadLine();
}
else
{
for (int j = 0; j < 5; j++) swt.WriteLine(sr.ReadLine());
}
}
swt.Flush();
sr.Close();
fr.Close();
swt.Close();
fwt.Close();
FileStream fw = new FileStream(Server.MapPath("temp.txt"), FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fw);
FileStream frt = new FileStream(Server.MapPath("abcd.txt"), FileMode.Open, FileAccess.Read);
StreamReader srt = new StreamReader(frt);
while (!srt.EndOfStream) sw.WriteLine(srt.ReadLine());
sw.Flush();
sw.Close();
fw.Close();
frt.Close();
srt.Close();
if (position == total)
{
total--;
position = total;
            }
else
{
total--;
}
read_text(position);
}
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();
TextBox2Text = sr.ReadLine();
TextBox3.Text = sr.ReadLine();
TextBox4.Text = sr.ReadLine();
sr.ReadLine();
}
sr.Close();
fs.Close();
}



No comments:

Post a Comment