Training
Module
Get started with file input and output - Training
Learn how to manage local files and directories using the System.IO classes, and how to store and retrieve C# objects using CSV files and the StreamReader and StreamWriter classes.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
Module
Get started with file input and output - Training
Learn how to manage local files and directories using the System.IO classes, and how to store and retrieve C# objects using CSV files and the StreamReader and StreamWriter classes.
Documentation
How to: Write text to a file - .NET
Learn ways to write or append text to a file for a .NET app. Use methods from the StreamWriter or File classes to write text synchronously or asynchronously.
Read from and write to a text file by Visual C# - C#
This article describes how to read from and write to a text file by using Visual C#. This article also provides some sample steps to explain related information.