To read a line from a text file in C#, you can use the StreamReader
class. Here's an example code snippet:
main.cs443 chars24 lines
In this example, we first create a StreamReader
instance with the path to the text file we want to read. Then we use the ReadLine
method of the StreamReader
to read the first line of the file. The using
statement is used to automatically close the StreamReader
after we're done with it.
Note that the ReadLine
method will return null
if it reaches the end of the file, so we need to check for that before trying to do something with the line we've read.
gistlibby LogSnag