add

Thursday, 26 September 2013

Write and read text to/from a text file in asp.net

 string path = Server.MapPath("~/");
            TextWriter tw = new StreamWriter(path+"Textsample.txt",true);
            // write a line of text to the file
            tw.WriteLine(txtresult.Text);

            // close the stream
            tw.Close();
       txtfieldname.Text = "";
       txtvalue.Text = "";
       txtresult.Text = "";
       StreamReader tr = new StreamReader(path + "Textsample.txt");
       //TextReader tr = new TextReader(path + "Textsample.txt");
       // read a line of text
       //Console.WriteLine(tr.ReadLine());
       var lineCount = File.ReadAllLines(path + "Textsample.txt").Length;
       //Response.Write(lineCount);
       for (int i = 0; i < lineCount; i++)
       {
           txtresult.Text += tr.ReadLine();
       }
       // close the stream
       tr.Close();

No comments:

Post a Comment