1. Code
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.IO;4 usingSystem.Linq;5 usingSystem.Text;6 usingSystem.Threading.Tasks;7 8 namespaceConsoleApplication49 {Ten class Program One { A Static voidMain (string[] args) - { - using(StreamReader Sreader =NewStreamReader (@"1.txt", Encoding.default)) the { - stringALine; - //controls whether a while loop makes a variable, true print text, false jumps out of the loop - BOOLCondition =true; + - while(true) + { A atALine =sreader.readline (); - - //Aline=null launched the text read, then the control amount of condition combined if statement action is to jump out of the loop - //if the text is not finished reading, then the function of the condition union if statement is to output the read text - if(aline==NULL) - { inCondition =false; - } to + if(condition) - { the Console.WriteLine (aLine); * } $ ElsePanax Notoginseng { - Break; the } + } A the + } - Console.readkey (); $ } $ } -}
2. txt file
3. Effect
You can also make improvements to the code, using the members that the class comes with
1. Code
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.IO;4 usingSystem.Linq;5 usingSystem.Text;6 usingSystem.Threading.Tasks;7 8 namespaceConsoleApplication49 {Ten class Program One { A Static voidMain (string[] args) - { - using(StreamReader Sreader =NewStreamReader (@"1.txt", Encoding.default)) the { - //if not to the end - while(!Sreader.endofstream) - { + Console.WriteLine (Sreader.readline ()); - } + } A Console.readkey (); at } - } -}
2. Effect
The ReadLine method in the C # console base StreamReader reads all the lines of the specified TXT file