WindowsRuntimeStreamExtensions 클래스

정의

Windows 런타임의 스트림과 Windows 스토어 앱용 .NET의 관리형 스트림 간 변환을 위한 확장 메서드를 포함합니다.

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
상속
WindowsRuntimeStreamExtensions
특성

예제

다음 예제에서는 다음을 사용하는 AsStreamForWrite 방법을 보여줍니다. 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

다음은 이전 예제와 연결된 XAML 코드입니다.

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

설명

이러한 확장 방법은 Windows 스토어 앱을 개발할 때만 사용할 수 있습니다. 이 방법은 Windows 스토어 앱에서 스트림을 사용하는 편리한 방법을 제공합니다. 클래스의 인스턴스를 WindowsRuntimeStreamExtensions 만들지 않습니다. 대신 인터페이스 및 IOutputStream 클래스의 IInputStream 인스턴스에서 이러한 메서드를 Stream 사용합니다.

WindowsRuntimeStreamExtensions 클래스에는 관리 Stream 되는 개체를 Windows 런타임 스트림으로 변환하는 두 가지 메서드가 포함되어 있습니다.

클래스에는 WindowsRuntimeStreamExtensions Windows 런타임 스트림을 개체로 변환하기 위한 세 가지 오버로드된 메서드가 Stream 포함되어 있습니다.

.NET Framework 4.5.1 WindowsRuntimeStreamExtensions 부터 클래스에는 스트림을 Windows 런타임 RandomAccessStream변환하는 메서드가 포함되어 있습니다.

메서드

AsInputStream(Stream)

Windows 스토어 앱용 .NET의 관리형 스트림을 Windows 런타임의 입력 스트림으로 변환합니다.

AsOutputStream(Stream)

Windows 스토어 앱용 .NET의 관리형 스트림을 Windows 런타임의 출력 스트림으로 변환합니다.

AsRandomAccessStream(Stream)

지정된 스트림을 임의 액세스 스트림으로 변환합니다.

AsStream(IRandomAccessStream)

Windows 런타임의 임의 액세스 스트림을 Windows 스토어 앱용 .NET의 관리형 스트림으로 변환합니다.

AsStream(IRandomAccessStream, Int32)

지정된 버퍼 크기를 사용하여 Windows 런타임의 임의 액세스 스트림을 Windows 스토어 앱용 .NET의 관리형 스트림으로 변환합니다.

AsStreamForRead(IInputStream)

Windows 런타임의 입력 스트림을 Windows 스토어 앱용 .NET의 관리형 스트림으로 변환합니다.

AsStreamForRead(IInputStream, Int32)

지정된 버퍼 크기를 사용하여 Windows 런타임의 입력 스트림을 Windows 8.x 스토어 앱용 .NET의 관리형 스트림으로 변환합니다.

AsStreamForWrite(IOutputStream)

Windows 런타임의 출력 스트림을 Windows 8.x 스토어 앱용 .NET의 관리형 스트림으로 변환합니다.

AsStreamForWrite(IOutputStream, Int32)

지정된 버퍼 크기를 사용하여 Windows 런타임의 출력 스트림을 Windows 스토어 앱용 .NET의 관리형 스트림으로 변환합니다.

적용 대상