Skip to content

Latest commit

 

History

History
60 lines (44 loc) · 4.88 KB

File metadata and controls

60 lines (44 loc) · 4.88 KB
title Asynchronous File I/O
description Read about asynchronous file I/O in .NET. Learn async methods to simplify asynchronous operations, such as ReadAsync, WriteAsync, and more.
ms.date 08/06/2026
dev_langs
csharp
vb
helpviewer_keywords
streams, synchronous streams
asynchronous I/O
synchronous I/O
streams, asynchronous streams
I/O [.NET], asynchronous I/O
Stream class, synchronous I/O
data streams, asynchronous streams
Stream class, asynchronous I/O
multiple I/O requests
data streams, synchronous streams
ms.assetid dbdd55e7-d6b9-4f9e-8abb-ab0edd4457f7

Asynchronous File I/O

Asynchronous operations enable you to perform resource-intensive I/O operations without blocking the main thread. This performance consideration is particularly important in a Windows 8.x Store app or desktop app where a time-consuming stream operation can block the UI thread and make your app appear as if it is not working.

The I/O types include async methods to simplify asynchronous operations. An async method contains Async in its name, such as xref:System.IO.Stream.ReadAsync*, xref:System.IO.Stream.WriteAsync*, xref:System.IO.Stream.CopyToAsync*, xref:System.IO.Stream.FlushAsync*, xref:System.IO.TextReader.ReadLineAsync*, and xref:System.IO.TextReader.ReadToEndAsync*. These async methods are implemented on stream classes, such as xref:System.IO.Stream, xref:System.IO.FileStream, and xref:System.IO.MemoryStream, and on classes that are used for reading from or writing to streams, such xref:System.IO.TextReader and xref:System.IO.TextWriter.

Important

In .NET Framework 4 and earlier versions, you have to use methods such as xref:System.IO.Stream.BeginRead* and xref:System.IO.Stream.EndRead* to implement asynchronous I/O operations. These methods are still available in current .NET versions to support legacy code; however, the async methods help you implement asynchronous I/O operations more easily.

C# and Visual Basic each have two keywords for asynchronous programming:

  • Async (Visual Basic) or async (C#) modifier, which is used to mark a method that contains an asynchronous operation.

  • Await (Visual Basic) or await (C#) operator, which is applied to the result of an async method.

To implement asynchronous I/O operations, use these keywords in conjunction with the async methods, as shown in the following examples. For more information, see Asynchronous programming with async and await (C#) or Asynchronous Programming with Async and Await (Visual Basic).

The following example demonstrates how to use two xref:System.IO.FileStream objects to copy files asynchronously from one directory to another. Notice that the xref:System.Web.UI.WebControls.Button.Click event handler for the xref:System.Windows.Controls.Button control is marked with the async modifier because it calls an asynchronous method.

[!code-csharpAsynchronous_File_IO_async#1] [!code-vbAsynchronous_File_IO_async#1]

The next example is similar to the previous one but uses xref:System.IO.StreamReader and xref:System.IO.StreamWriter objects to read and write the contents of a text file asynchronously.

[!code-csharpAsynchronous_File_IO_async#2] [!code-vbAsynchronous_File_IO_async#2]

The next example shows the code-behind file and the XAML file that are used to open a file as a xref:System.IO.Stream in a Windows 8.x Store app, and read its contents by using an instance of the xref:System.IO.StreamReader class. It uses asynchronous methods to open the file as a stream and to read its contents.

[!code-csharpSystem.IO.WindowsRuntimeStorageExtensions#2] [!code-vbSystem.IO.WindowsRuntimeStorageExtensions#2]

[!code-xamlSystem.IO.WindowsRuntimeStorageExtensions#1]

See also