How to read all lines of a file using async and await in VB.NET

Marc Menzel 101 Reputation points
2025-04-09T02:33:58.24+00:00

How can I convert the Following Code into code that uses async and await?

`Public Function GetFileText()`

Return File.ReadAllLines(FileItemListPath)

End Function

Dim ListLines() As String = GetFileText()
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,827 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 121.3K Reputation points
    2025-04-09T06:55:26.96+00:00

    Try this:

    Public Async Function GetFileTextAsync() As Task(Of String())
        Return Await File.ReadAllLinesAsync(FileItemListPath)
    End Function
    
    ' usage inside an Async function:
    Dim ListLines As String() = Await GetFileTextAsync()
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.