Use streamreader to read information in TXT text

Source: Internet
Author: User

Protected void button#click (Object sender, eventargs E)
String filename = This. fileupload1.filename. Trim ();
This. gridview1.datasource = loaddata (filename );
This. gridview1.databind ();
Public datatable loaddata (string filename)
Datatable dt = new datatable ();
DT. Columns. Add ("Lucky Draw user", typeof (string ));
DT. Columns. Add ("Lottery time", typeof (string ));
DT. Columns. Add ("win?", typeof (string ));
DT. Columns. Add ("remaining lottery Times", typeof (string ));
String Path = server. mappath (filename );

Fileupload1.saveas (PATH );

Streamreader reader = new streamreader (path, system. Text. encoding. Default); // open the file and read the text into the stream. // use the default system encoding.
This. label1.text = reader. readtoend ();
This. label1.visible = false;
String [] arrstr = This. label1.text. Split (New char [] {'/R','/N'}); // put the total number of rows in the array

String [] cells;
For (INT I = 1; I <arrstr. length; I ++)

Cells = arrstr [I]. tostring (). Split ('/t'); // truncates each field in a row

For (Int J = 0; j <cells. length; j ++)
Datarow rows = DT. newrow ();
Rows ["lottery user"] = cells [0];
Rows ["Lottery time"] = cells [1];
Rows ["win?"] = cells [2];
Rows ["remaining lottery times"] = cells [3];
DT. Rows. Add (rows );


Reader. Close ();

Return DT;


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: 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.