Permalink
Fetching contributors…
Cannot retrieve contributors at this time
85 lines (61 sloc) 10.1 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic ms.assetid caps.latest.revision author ms.author manager
How to: Convert Between .NET Framework Streams and Windows Runtime Streams
03/30/2017
.net
dotnet-standard
article
23a763ea-8348-4244-9f8c-a4280b870b47
15
mairaw
mairaw
wpickett

How to: Convert Between .NET Framework Streams and Windows Runtime Streams

The .NET Framework for Windows Store apps is a subset of the full .NET Framework. Because of security and other requirements for Windows Store apps, you cannot use the full set of .NET Framework APIs to open and read files. For more information, see .NET for Windows Store apps overview. However, you may want to use .NET Framework APIs for other stream manipulation operations. To manipulate these streams, you may find it necessary to convert between a .NET Framework stream type such as xref:System.IO.MemoryStream or xref:System.IO.FileStream, and a Windows Runtime stream such as IInputStream, IOutputStream, or IRandomAccessStream.

The System.IO.WindowsRuntimeStreamExtensions class contains methods that make these conversions easy. However, there are underlying differences between streams in the .NET Framework and the Windows Runtime that will affect the results of using these methods. The details are described in the following sections.

Converting from a Windows Runtime stream to a .NET Framework stream

You can convert from a Windows Runtime stream to a .NET Framework stream by using one of the following System.IO.WindowsRuntimeStreamExtensions methods:

`System.IO.WindowsRuntimeStreamExtensions.AsStream`

Converts a random-access stream in the Windows Runtime to a managed stream in the .NET for Windows Store apps subset.

`System.IO.WindowsRuntimeStreamExtensions.AsStreamForWrite`

Converts an output stream in the Windows Runtime to a managed stream in the .NET for Windows Store apps subset.

`System.IO.WindowsRuntimeStreamExtensions.AsStreamForRead`

Converts an input stream in the Windows Runtime to a managed stream in the .NET for Windows Store apps subset.

The Windows Runtime offers stream types that support reading only, writing only or reading and writing, and these capabilities are maintained when you convert a Windows Runtime stream to a .NET Framework stream. Furthermore, if you convert a Windows Runtime stream to a .NET Framework stream and back, you get the original Windows Runtime instance back. It’s best practice to use the conversion method that matches the capabilities of the Windows Runtime stream you would like to convert. However, since IRandomAccessStream is readable and writeable (it implements both IOutputStream and IInputStream), you can use any of the conversion methods and the capabilities of the original stream are maintained. For example, using System.IO.WindowsRuntimeStreamExtensions.AsStreamForRead to convert an IRandomAccessStream will not limit the converted .NET Framework stream to being readable only; it will also be writable.

To convert from a Windows Runtime random-access stream to a .NET Framework stream

Converting from a .NET Framework stream to a Windows Runtime stream

You can convert from a .NET Framework stream to a Windows Runtime stream by using one of the following System.IO.WindowsRuntimeStreamExtensions methods:

`System.IO.WindowsRuntimeStreamExtensions.AsInputStream`

Converts a managed stream in the .NET for Windows Store apps subset to an input stream in the Windows Runtime.

`System.IO.WindowsRuntimeStreamExtensions.AsOutputStream`

Converts a managed stream in the .NET for Windows Store apps subset to an output stream in the Windows Runtime.

AsRandomAccessStream
Converts a managed stream in the .NET for Windows Store apps subset to a random-access stream that can be used for reading or writing in the Windows Runtime.

When you convert a .NET Framework stream to a Windows Runtime stream, the capabilities of the converted stream will depend on the original stream. For example, if the original stream supports both reading and writing, and you call System.IO.WindowsRuntimeStreamExtensions.AsInputStream to convert the stream, the returned type will be an IRandomAccessStream, which implements IInputStream and IOutputStream, and supports reading and writing

.NET Framework streams do not support cloning, even after conversion. This means that if you convert a .NET Framework stream to a Windows Runtime stream and call GetInputStreamAt or GetOutputStreamAt, which call CloneStream or call CloneStream directly, an exception will occur.

To convert from a .NET Framework stream to a Windows Runtime random-access stream

See Also

Quickstart: Reading and writing a file (Windows)
.NET for Windows Store apps overview
.NET for Windows Store apps – supported APIs