1. Using the file class to return an array of string[] with each row, it is highly recommended to use
Public Partialform1:form{string[] lines; PublicForm1 () {InitializeComponent (); } Private voidForm1_Load (Objectsender, System.EventArgs e) {Lines= File.ReadAllLines ("TextFile1.txt"); //The first line is displayed in the TextBox1 if(lines. Length >0) {TextBox1.Text= lines[0]; } //the second line is displayed in the TextBox2 if(lines. Length >1) {TextBox2.Text= lines[1]; } }}
2. c#2.0 before, FileStream, not recommended
usingSystem.IO Public Partialform1:form{List<string>lines; PublicForm1 () {InitializeComponent (); //hold a collection of all rowsLines =Newlist<string>(); } Private voidForm1_Load (Objectsender, System.EventArgs e) {FileStream FS=NewFileStream ("TextFile1.txt", FileMode.Open); StreamReader Rd=NewStreamReader (FS); strings; //reads all the lines of the file into the List<string> collection while(S= Rd. ReadLine ())! =NULL) {lines. ADD (s); } Rd. Close (); Fs. Close (); //The first line is displayed in the TextBox1 if(lines. Count >0) {TextBox1.Text= lines[0]; } //the second line is displayed in the TextBox2 if(lines. Count >1) {TextBox2.Text= lines[1]; } }}
C # IO reads the specified number of rows