WindowsRuntimeStreamExtensions Classe

Definição

Contém métodos de extensão para conversão entre fluxos no Windows Runtime e em fluxos gerenciados no .NET para Aplicativos da Windows Store.

public ref class WindowsRuntimeStreamExtensions abstract sealed
public static class WindowsRuntimeStreamExtensions
[System.Security.SecurityCritical]
public static class WindowsRuntimeStreamExtensions
type WindowsRuntimeStreamExtensions = class
[<System.Security.SecurityCritical>]
type WindowsRuntimeStreamExtensions = class
Public Module WindowsRuntimeStreamExtensions
Herança
WindowsRuntimeStreamExtensions
Atributos

Exemplos

O exemplo a seguir mostra como usar o AsStreamForWrite e AsStreamForRead

using System;
using System.IO;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace ExampleApplication
{
    public sealed partial class BlankPage : Page
    {
        public BlankPage()
        {
            this.InitializeComponent();
        }

        private async void CreateButton_Click(object sender, RoutedEventArgs e)
        {
            StorageFile newFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("testfile.txt");
            var streamNewFile = await newFile.OpenAsync(FileAccessMode.ReadWrite);

            using (var outputNewFile = streamNewFile.GetOutputStreamAt(0))
            {
                using (StreamWriter writer = new StreamWriter(outputNewFile.AsStreamForWrite()))
                {
                    await writer.WriteLineAsync("content for new file");
                    await writer.WriteLineAsync(UserText.Text);
                }
            }
        }

        private async void VerifyButton_Click(object sender, RoutedEventArgs e)
        {
            StorageFile openedFile = await ApplicationData.Current.LocalFolder.GetFileAsync("testfile.txt");
            var streamOpenedFile = await openedFile.OpenAsync(FileAccessMode.Read);

            using (var inputOpenedFile = streamOpenedFile.GetInputStreamAt(0))
            {
                using (StreamReader reader = new StreamReader(inputOpenedFile.AsStreamForRead()))
                {
                    Results.Text = await reader.ReadToEndAsync();
                }
            }
        }
    }
}
Imports System.IO
Imports Windows.Storage

NotInheritable Public Class BlankPage
    Inherits Page

    Private Async Sub CreateButton_Click(sender As Object, e As RoutedEventArgs)
        Dim newFile As StorageFile = Await ApplicationData.Current.LocalFolder.CreateFileAsync("testfile.txt")
        Dim streamNewFile = Await newFile.OpenAsync(FileAccessMode.ReadWrite)

        Using outputNewFile = streamNewFile.GetOutputStreamAt(0)
            Using writer As StreamWriter = New StreamWriter(outputNewFile.AsStreamForWrite())
                Await writer.WriteLineAsync("content for new file")
                Await writer.WriteLineAsync(UserText.Text)
            End Using
        End Using
    End Sub

    Private Async Sub VerifyButton_Click(sender As Object, e As RoutedEventArgs)
        Dim openedFile As StorageFile = Await ApplicationData.Current.LocalFolder.GetFileAsync("testfile.txt")
        Dim streamOpenedFile = Await openedFile.OpenAsync(FileAccessMode.Read)

        Using inputOpenedFile = streamOpenedFile.GetInputStreamAt(0)

            Using reader As StreamReader = New StreamReader(inputOpenedFile.AsStreamForRead())
                Results.Text = Await reader.ReadToEndAsync()
            End Using
        End Using
    End Sub
End Class

Aqui está o código XAML associado ao exemplo anterior.

<Page
    x:Class="ExampleApplication.BlankPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ExampleApplication"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel Background="{StaticResource ApplicationPageBackgroundBrush}" VerticalAlignment="Center" HorizontalAlignment="Center">
        <TextBlock Text="Provide text to write to file:"></TextBlock>
        <TextBox Name="UserText" Width="400"></TextBox>
        <Button Name="CreateButton" Content="Create File" Click="CreateButton_Click"></Button>
        <Button Name="VerifyButton" Content="Verify Contents" Click="VerifyButton_Click"></Button>
        <TextBlock Name="Results"></TextBlock>
    </StackPanel>
</Page>

Comentários

Esses métodos de extensão só estão disponíveis quando você desenvolve aplicativos Windows Store. Os métodos fornecem maneiras convenientes de trabalhar com fluxos em aplicativos da Windows Store. Você não cria uma instância da WindowsRuntimeStreamExtensions classe; em vez disso, usa esses métodos de instâncias e IInputStream IOutputStream interfaces e da Stream classe.

A WindowsRuntimeStreamExtensions classe contém dois métodos para converter um objeto gerenciado Stream em um fluxo no Windows Runtime:

A WindowsRuntimeStreamExtensions classe contém três métodos sobrecarregados para converter um fluxo no Windows Runtime em um Stream objeto:

A partir do .NET Framework 4.5.1, a WindowsRuntimeStreamExtensions classe contém um método para converter um fluxo em um Windows RuntimeRandomAccessStream:

Métodos

AsInputStream(Stream)

Converte um fluxo gerenciado no .NET para Aplicativos da Windows Store em um fluxo de entrada no Windows Runtime.

AsOutputStream(Stream)

Converte um fluxo gerenciado no .NET para Aplicativos da Windows Store em um fluxo de saída no Windows Runtime.

AsRandomAccessStream(Stream)

Converte o fluxo especificado em um fluxo de acesso aleatório.

AsStream(IRandomAccessStream)

Converte um fluxo de acesso aleatório no Windows Runtime em um fluxo gerenciado no .NET para Aplicativos da Windows Store.

AsStream(IRandomAccessStream, Int32)

Converte um fluxo de acesso aleatório no Windows Runtime em um fluxo gerenciado no .NET para Aplicativos da Windows Store usando o tamanho do buffer especificado.

AsStreamForRead(IInputStream)

Converte um fluxo de entrada no Windows Runtime em um fluxo gerenciado no .NET para Aplicativos da Windows Store.

AsStreamForRead(IInputStream, Int32)

Converte um fluxo de entrada no Windows Runtime em um fluxo gerenciado no .NET para aplicativos da Windows 8.x Store usando o tamanho do buffer especificado.

AsStreamForWrite(IOutputStream)

Converte um fluxo de saída no Windows Runtime em um fluxo gerenciado no .NET para aplicativos da Windows 8.x Store.

AsStreamForWrite(IOutputStream, Int32)

Converte um fluxo de saída no Windows Runtime em um fluxo gerenciado no .NET para Aplicativos da Windows Store usando o tamanho do buffer especificado.

Aplica-se a