Training
Learning path
Access local files asynchronously - Training
Learn how to manage local files using the System.IO namespace and how to asynchronously back up and restore application data using the System.Text.Json namespace.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The following examples show how to read text synchronously and asynchronously from a text file using .NET for desktop apps. In both examples, when you create the instance of the StreamReader class, you provide the relative or absolute path to the file.
Note
These code examples don't apply to Universal Windows (UWP) apps because the Windows runtime provides different stream types for reading and writing to files. For more information, see UWP work with files. For examples that show how to convert between .NET Framework streams and Windows Runtime streams, see How to: Convert between .NET Framework streams and Windows Runtime streams.
Create a text file named TestFile.txt in the same folder as the app.
Add some content to the text file. The examples in this article write the content of the text file to the console.
The following example shows a synchronous read operation within a console app. The file contents are read and stored in a string variable, which is then written to the console.
try
{
// Open the text file using a stream reader.
using StreamReader reader = new("TestFile.txt");
// Read the stream as a string.
string text = reader.ReadToEnd();
// Write the text to the console.
Console.WriteLine(text);
}
catch (IOException e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
Try
' Open the text file using a stream reader.
Using reader As New StreamReader("TestFile.txt")
' Read the stream as a string.
Dim text As String = reader.ReadToEnd()
' Write the text to the console.
Console.WriteLine(text)
End Using
Catch ex As IOException
Console.WriteLine("The file could not be read:")
Console.WriteLine(ex.Message)
End Try
The following example shows an asynchronous read operation within a console app. The file contents are read and stored in a string variable, which is then written to the console.
try
{
// Open the text file using a stream reader.
using StreamReader reader = new("TestFile.txt");
// Read the stream as a string.
string text = await reader.ReadToEndAsync();
// Write the text to the console.
Console.WriteLine(text);
}
catch (IOException e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
Try
' Open the text file using a stream reader.
Using reader As New StreamReader("TestFile.txt")
' Read the stream as a string.
Dim text As String = Await reader.ReadToEndAsync()
' Write the text to the console.
Console.WriteLine(text)
End Using
Catch ex As IOException
Console.WriteLine("The file could not be read:")
Console.WriteLine(ex.Message)
End Try
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Training
Learning path
Access local files asynchronously - Training
Learn how to manage local files using the System.IO namespace and how to asynchronously back up and restore application data using the System.Text.Json namespace.