Read html file and text file in ASP.NET

The following code-snippet tell how to read file(s):

Add main namespace for this operation


using System.IO;


Read html file

string file = Server.MapPath ("abc.html");
StreamReader sr;
FileInfo fi = new FileInfo(file);
string input = "<pre>";
if(File.Exists(file))
{
sr = File.OpenText(file);
input += Server.HtmlEncode(sr.ReadToEnd());
sr.Close();
}
input += "</pre>";
Label1.Text = input;


Read text file

StreamReader sr = File.OpenText(Server.MapPath("abc.txt"));
string strContents = sr.ReadToEnd();

//To display normal raw contents
Response.Write(strContents);

//To handle Carriage returns
Response.Write(strContents.Replace("\n" , "<br>"));

sr.Close();


Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: