C # Read txt Notepad file by line

Source: Internet
Author: User
The student list is stored in Notepad, and a student's name is written on one line

Read Notepad files through FileStream, StreamReader, and store the contents in a DataTable


<summary>
///DataTable Display selected student list
///</summary>
///<returns></returns> Public
static DataTable stulist ()
{
	DataTable dt = new DataTable ();
	Dt. Columns.Add ("Student name");
	String Sfilepath = String. Empty;

	The path of the student list
	Sfilepath = Parameters.g_sstulistpath + Frm.cblClass.SelectedItem;

	Determine if the student list exists if
	(! File.exists (Sfilepath))
	{
		MessageBox.Show ("The Student list file does not exist, please check for errors.") ");
		return dt;
	}

	String sstuname = String. Empty;
	FileStream fs = new FileStream (Sfilepath, FileMode.Open);
	"GB2312" is used to display Chinese characters, to write other words, will display garbled
	StreamReader reader = new StreamReader (FS, unicodeencoding.getencoding (" GB2312 "));

	One line reads while
	(sstuname = reader. ReadLine ())!= null)
	{
		sstuname = Sstuname.trim (). ToString ();
		DataRow dr = dt. NewRow ();
		dr["Student Name" = Sstuname;
		Dt. Rows.Add (DR);
	}

	m_dtstu = DT;
	
	Closes the file stream
	fs. Close ();
	return DT;
}


Related Article

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.