This documentation is archived and is not being maintained.
How to: Read Text from a File
.NET Framework 2.0
The following code examples show how to read text from a text file. The second example notifies you when the end of the file is detected. This functionality can also be achieved by using the ReadAllLines or ReadAllText methods.
Example
Robust Programming
This code creates a StreamReader that points to MyFile.txt through a call to File.OpenText. StreamReader.ReadLine returns each line as a string. When there are no more characters to read, a message is displayed to that effect, and the stream is closed.
Note |
|---|
| Visual Basic users may choose to use the methods and properties provided by the My.Computer.FileSystem object for file I/O. For more information, see My.Computer.FileSystem Object. |
See Also
Tasks
How to: Create a Directory ListingHow to: Read and Write to a Newly Created Data File
How to: Open and Append to a Log File
How to: Write Text to a File
How to: Read Characters from a String
How to: Write Characters to a String
Reference
StreamReaderFile.OpenText
StreamReader.ReadLine
Concepts
Basic File I/O
Show:
Note