Skip to content

Latest commit

 

History

History
60 lines (45 loc) · 3.13 KB

how-to-read-text-from-a-file.md

File metadata and controls

60 lines (45 loc) · 3.13 KB
title description ms.date ms.custom dev_langs helpviewer_keywords ms.assetid
Read text from a file
In this article, see examples of how to read text synchronously or asynchronously from a text file, using the StreamReader class in .NET for desktop apps.
05/09/2024
devdivchpfy22
csharp
vb
streams, reading text from files
reading text files
reading data, text files
data streams, reading text from files
I/O [.NET], reading text from files
ed180baa-dfc6-4c69-a725-46e87edafb27

Read text from a file

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 xref:System.IO.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.

Prerequisites

  • 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.

Read a file

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.

  1. Create a xref:System.IO.StreamReader instance.
  2. Call the xref:System.IO.StreamReader.ReadToEnd?displayProperty=nameWithType method and assign the result to a string.
  3. Write the output to the console.

:::code language="csharp" source="snippets/how-to-read-text-from-a-file/csharp/Program.cs" id="sync"::: :::code language="vb" source="snippets/how-to-read-text-from-a-file/vb/Program.vb" id="sync":::

Read a file asynchronously

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.

  1. Create a xref:System.IO.StreamReader instance.
  2. Await the xref:System.IO.StreamReader.ReadToEndAsync?displayProperty=nameWithType method and assign the result to a string.
  3. Write the output to the console.

:::code language="csharp" source="snippets/how-to-read-text-from-a-file/csharp/Program.cs" id="async"::: :::code language="vb" source="snippets/how-to-read-text-from-a-file/vb/Program.vb" id="async":::

Related content