作者:VB.NET开发
项目:System.I
' 导入命名空间
Imports System.IO
Class Test
Public Shared Sub Main()
Try
' Create an instance of StreamReader to read from a file.
Dim sr As StreamReader = New StreamReader("TestFile.txt")
Dim line As String
' Read and display the lines from the file until the end
' of the file is reached.
Do
line = sr.ReadLine()
Console.WriteLine(Line)
Loop Until line Is Nothing
sr.Close()
Catch E As Exception
' Let the user know what went wrong.
Console.WriteLine("The file could not be read:")
Console.WriteLine(E.Message)
End Try
End Sub
End Class
作者:VB.NET开发
项目:System.I
' 导入命名空间
Imports System.IO
Imports System.Threading.Tasks
Module Example
Public Sub Main()
ReadAndDisplayFilesAsync()
End Sub
Private Async Sub ReadAndDisplayFilesAsync()
Dim filename As String = "TestFile1.txt"
Dim buffer() As Char
Using sr As New StreamReader(filename)
ReDim buffer(CInt(sr.BaseStream.Length))
Await sr.ReadAsync(buffer, 0, CInt(sr.BaseStream.Length))
End Using
Console.WriteLine(New String(buffer))
End Sub
End Module