Export (0) Print
Expand All

WindowsRuntimeStreamExtensions.AsInputStream Method

.NET Framework 4.5

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

This API is not CLS-compliant. 

Namespace:  System.IO
Assembly:  System.Runtime.WindowsRuntime (in System.Runtime.WindowsRuntime.dll)

public static IInputStream AsInputStream(
	this Stream stream


Type: System.IO.Stream

The stream to convert.

Return Value

Type: IInputStream
A Windows Runtime IInputStream object that represents the converted stream.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type Stream. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).


stream is null.


The stream does not support reading.

The following example shows how to use the AsInputStream and AsOutputStream methods to convert a managed stream to and from a stream in the Windows Runtime.

using System;
using System.IO;
using System.Text;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace ExampleApplication
    public sealed partial class BlankPage : Page
        System.Text.UnicodeEncoding ue;
        byte[] bytesToWrite;
        byte[] bytesToAdd;
        int totalBytes;

        public BlankPage()

        protected override void OnNavigatedTo(NavigationEventArgs e)
            ue = new System.Text.UnicodeEncoding();
            bytesToWrite = ue.GetBytes("example text to write to memory stream");
            bytesToAdd = ue.GetBytes("text added through datawriter");
            totalBytes = bytesToWrite.Length + bytesToAdd.Length;

        private async void CreateButton_Click(object sender, RoutedEventArgs e)
            byte[] bytesRead = new byte[totalBytes];
            using (MemoryStream memStream = new MemoryStream(totalBytes))
                await memStream.WriteAsync(bytesToWrite, 0, bytesToWrite.Length);

                DataWriter writer = new DataWriter(memStream.AsOutputStream());
                await writer.StoreAsync();

                memStream.Seek(0, SeekOrigin.Begin);

                DataReader reader = new DataReader(memStream.AsInputStream());
                await reader.LoadAsync((uint)totalBytes);
                Results.Text = ue.GetString(bytesRead, 0, bytesRead.Length);

Here's the XAML code that is associated with the previous example.


    <StackPanel Background="{StaticResource ApplicationPageBackgroundBrush}" VerticalAlignment="Center" HorizontalAlignment="Center">
        <Button Name="CreateButton" Content="Write and read with stream" Click="CreateButton_Click"></Button>
        <TextBlock Name="Results"></TextBlock>

.NET Framework

Supported in: 4.6, 4.5

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone Silverlight 8

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft