Asp.net simple example of reading text files (by line)

Source: Internet
Author: User

This article summarizes two examples of reading text files. One is to read all the file content at a time and the other is to read the content by line. For more information, see.

First, add the using System. IO namespace.

Code Section:

The Code is as follows: Copy code

Public string readfile (string paths)
{
StreamReader sr = new StreamReader (Server. MapPath (paths), System. Text. Encoding. Default );
String input = sr. ReadToEnd ();
Return input;
}

The above is the core code, and the complete file code is read.

 

The Code is as follows: Copy code

Using System;
Using System. Collections;
Using System. Configuration;
Using System. Data;
Using System. Linq;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Xml. Linq;
Using System. IO;
Namespace test
{
Public partial class Text: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
Response. Write (GetInterIDList ("asp.txt "));
}
// Read the contents of the txt file
Public string GetInterIDList (string strfile)
{
String strout;
Strout = "";
If (! File. Exists (System. Web. HttpContext. Current. Server. MapPath (strfile )))
{
}
Else
{
StreamReader sr = new StreamReader (System. Web. HttpContext. Current. Server. MapPath (strfile), System. Text. Encoding. Default );
String input = sr. ReadToEnd ();
Sr. Close ();
Strout = input;
}
Return strout;
}

}
}

 

One row reads the current file

Example

The Code is as follows: Copy code

Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;

Using System. IO;
Using System. Text;

Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! This. IsPostBack)
{
String Path = Server. MapPath ("4.txt ");
InsertStr (Path );
}
}

Public void insertStr (string Path)
{
String strLine = "";
Int I = 0;
Try
{
StreamReader sr = new StreamReader (Path, Encoding. GetEncoding ("GB2312 "));

StrLine = sr. ReadLine ();

While (strLine! = Null)
{
Response. Write (strLine + "<br> ");
StrLine = sr. ReadLine ();
I ++;
}
Response. Write (I );
Sr. Dispose ();
Sr. Close ();
}
Catch
{
}


}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.