WindowsRuntimeStreamExtensions.AsStreamForRead Metodo

Definizione

Overload

AsStreamForRead(IInputStream)

Converte un flusso di input in Windows Runtime in un flusso gestito in .NET per app di Windows Store.Converts an input stream in the Windows Runtime to a managed stream in the .NET for Windows Store apps.

AsStreamForRead(IInputStream, Int32)

Converte un flusso di input in Windows Runtime in un flusso gestito in .NET per app di Windows 8.x Store usando le dimensioni del buffer specificate.Converts an input stream in the Windows Runtime to a managed stream in the .NET for Windows 8.x Store apps using the specified buffer size.

AsStreamForRead(IInputStream)

Importante

Questa API non è conforme a CLS.

Converte un flusso di input in Windows Runtime in un flusso gestito in .NET per app di Windows Store.Converts an input stream in the Windows Runtime to a managed stream in the .NET for Windows Store apps.

public:
[System::Runtime::CompilerServices::Extension]
 static System::IO::Stream ^ AsStreamForRead(Windows::Storage::Streams::IInputStream ^ windowsRuntimeStream);
[System.CLSCompliant(false)]
public static System.IO.Stream AsStreamForRead (this Windows.Storage.Streams.IInputStream windowsRuntimeStream);
[<System.CLSCompliant(false)>]
static member AsStreamForRead : Windows.Storage.Streams.IInputStream -> System.IO.Stream
<Extension()>
Public Function AsStreamForRead (windowsRuntimeStream As IInputStream) As Stream

Parametri

windowsRuntimeStream
IInputStream

Oggetto IInputStream di Windows Runtime da convertire.The Windows Runtime IInputStream object to convert.

Restituisce

Stream

Flusso convertito.The converted stream.

Attributi

Eccezioni

windowsRuntimeStream è null.windowsRuntimeStream is null.

Esempio

Nell'esempio seguente viene illustrato come utilizzare i AsStreamForWrite AsStreamForRead metodi e per convertire un flusso gestito da e verso un flusso nel Windows Runtime.The following example shows how to use the AsStreamForWrite and AsStreamForRead methods to convert a managed stream to and from a stream in the Windows Runtime.

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

Ecco il codice XAML associato all'esempio precedente.Here's the XAML code that is associated with the previous example.

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

Commenti

Nota

In Visual Basic e C# è possibile chiamare questo metodo come metodo di istanza su qualsiasi oggetto di tipo Stream .In Visual Basic and C#, you can call this method as an instance method on any object of type Stream. Per chiamare il metodo usando la sintassi del metodo di istanza, omettere il primo parametro.When you use instance method syntax to call this method, omit the first parameter. Per ulteriori informazioni, vedere metodi di estensione (Visual Basic) o metodi di estensione (Guida per programmatori C#).For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

Una dimensione del buffer predefinita di 16.384 byte viene utilizzata per la conversione del flusso.A default buffer size of 16,384 bytes is used when converting the stream. Per specificare una dimensione del buffer diversa, usare l' AsStreamForRead(IInputStream, Int32) Overload.To specify a different buffer size, use the AsStreamForRead(IInputStream, Int32) overload.

Si applica a

AsStreamForRead(IInputStream, Int32)

Importante

Questa API non è conforme a CLS.

Converte un flusso di input in Windows Runtime in un flusso gestito in .NET per app di Windows 8.x Store usando le dimensioni del buffer specificate.Converts an input stream in the Windows Runtime to a managed stream in the .NET for Windows 8.x Store apps using the specified buffer size.

public:
[System::Runtime::CompilerServices::Extension]
 static System::IO::Stream ^ AsStreamForRead(Windows::Storage::Streams::IInputStream ^ windowsRuntimeStream, int bufferSize);
[System.CLSCompliant(false)]
public static System.IO.Stream AsStreamForRead (this Windows.Storage.Streams.IInputStream windowsRuntimeStream, int bufferSize);
[<System.CLSCompliant(false)>]
static member AsStreamForRead : Windows.Storage.Streams.IInputStream * int -> System.IO.Stream
<Extension()>
Public Function AsStreamForRead (windowsRuntimeStream As IInputStream, bufferSize As Integer) As Stream

Parametri

windowsRuntimeStream
IInputStream

Oggetto di Windows Runtime da convertire.The Windows Runtime object to convert.

bufferSize
Int32

Dimensione del buffer, in byte.The size, in bytes, of the buffer. Questo valore non può essere negativo, ma può essere 0 (zero) per disabilitare la memorizzazione nel buffer.This value cannot be negative, but it can be 0 (zero) to disable buffering.

Restituisce

Stream

Flusso convertito.The converted stream.

Attributi

Eccezioni

windowsRuntimeStream è null.windowsRuntimeStream is null.

bufferSize è negativo.bufferSize is negative.

Commenti

Nota

In Visual Basic e C# è possibile chiamare questo metodo come metodo di istanza su qualsiasi oggetto di tipo Stream .In Visual Basic and C#, you can call this method as an instance method on any object of type Stream. Per chiamare il metodo usando la sintassi del metodo di istanza, omettere il primo parametro.When you use instance method syntax to call this method, omit the first parameter. Per ulteriori informazioni, vedere metodi di estensione (Visual Basic) o metodi di estensione (Guida per programmatori C#).For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

Usare questo metodo per specificare una dimensione del buffer durante la conversione del flusso.You use this method to specify a buffer size when converting the stream. Per usare le dimensioni predefinite del buffer di 16.384 byte, usare l' AsStreamForRead(IInputStream) Overload.To use the default buffer size of 16,384 bytes, use the AsStreamForRead(IInputStream) overload.

Nella maggior parte dei casi, il buffering migliora le prestazioni delle operazioni di flusso.In most situations, buffering improves the performance of stream operations. È possibile disabilitare la memorizzazione nel buffer impostando bufferSize su zero, ma è necessario eseguire questa operazione solo quando si è certi che la disabilitazione del buffering sia appropriata per la situazione specifica.You can disable buffering by setting bufferSize to zero, but you should do this only when you are sure that disabling buffering is appropriate for your situation.

Si applica a