Bewerken

Delen via


WindowsRuntimeStreamExtensions.AsStreamForWrite Method

Definition

Overloads

AsStreamForWrite(IOutputStream)

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

AsStreamForWrite(IOutputStream, Int32)

Converts an output stream in the Windows Runtime to a managed stream in the .NET for Windows Store apps using the specified buffer size.

AsStreamForWrite(IOutputStream)

Important

This API is not CLS-compliant.

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

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

Parameters

windowsRuntimeStream
IOutputStream

The Windows Runtime IOutputStream object to convert.

Returns

The converted stream.

Attributes

Exceptions

windowsRuntimeStream is null.

Examples

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

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>

Remarks

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

A default buffer size of 16,384 bytes is used when converting the stream. To specify a different buffer size, use the AsStreamForWrite(IOutputStream, Int32) overload.

Applies to

AsStreamForWrite(IOutputStream, Int32)

Important

This API is not CLS-compliant.

Converts an output stream in the Windows Runtime to a managed stream in the .NET for Windows Store apps using the specified buffer size.

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

Parameters

windowsRuntimeStream
IOutputStream

The Windows Runtime IOutputStream object to convert.

bufferSize
Int32

The size, in bytes, of the buffer. This value cannot be negative, but it can be 0 (zero) to disable buffering.

Returns

The converted stream.

Attributes

Exceptions

windowsRuntimeStream is null.

bufferSize is negative.

Remarks

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

You use this method to specify a buffer size when converting the stream. To use the default buffer size of 16,384 bytes, use the AsStreamForWrite(IOutputStream) overload.

In most situations, buffering improves the performance of stream operations. 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.

Applies to