語言

WindowsRuntimeStreamExtensions 類別

定義

包含在 Windows 執行階段 串流間轉換的擴充方法,以及在 .NET for Windows 商店應用程式中管理串流之間轉換。

public ref class WindowsRuntimeStreamExtensions abstract sealed
[System.Security.SecurityCritical]
public static class WindowsRuntimeStreamExtensions
[<System.Security.SecurityCritical>]
type WindowsRuntimeStreamExtensions = class
Public Module WindowsRuntimeStreamExtensions
繼承
WindowsRuntimeStreamExtensions
屬性

範例

以下範例說明如何使用 和AsStreamForWriteAsStreamForRead

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;而是從 和 <xref:Windows.Storage.Streams.IInputStream> 介面的實例<xref:Windows.Storage.Streams.IOutputStream>和Stream類別中使用這些方法。

WindowsRuntimeStreamExtensions 類別包含兩種將受管理的 Stream 物件轉換為 Windows 執行階段 中的串流的方法:

WindowsRuntimeStreamExtensions 類別包含三種超載方法,用於將 Windows 執行階段 中的串流轉換為 Stream 物件:

從 .NET Framework 4.5.1 開始, WindowsRuntimeStreamExtensions 類別包含一個將串流轉換為 Windows 執行階段 <xref:Windows.Storage.Streams.RandomAccessStream> 的方法:

方法

名稱 Description
AsInputStream(Stream)

將適用於 Windows 市集應用程式的 .NET 中的 Managed 資料流轉換為 Windows 執行時間中的輸入資料流。

AsOutputStream(Stream)

將適用於 Windows 市集應用程式的 .NET 中的 Managed 資料流轉換為 Windows 執行時間中的輸出資料流。

AsRandomAccessStream(Stream)

將指定的數據流轉換為隨機存取數據流。

AsStream(IRandomAccessStream, Int32)

將Windows 執行階段中的隨機存取串流轉換為.NET中管理的串流,適用於指定的緩衝區大小Windows商店應用程式。

AsStream(IRandomAccessStream)

將 Windows 執行階段 中的隨機存取串流轉換為 .NET for Windows 商店應用程式中的受管理串流。

AsStreamForRead(IInputStream, Int32)

將 Windows 執行階段 中的輸入串流轉換為 .NET 中的管理串流,適用於 Windows 8.x 商店應用程式,使用指定的緩衝區大小。

AsStreamForRead(IInputStream)

將 Windows 執行階段 中的輸入串流轉換為 .NET 中的管理串流,供 Windows Store 應用程式使用。

AsStreamForWrite(IOutputStream, Int32)

將 Windows 執行階段 中的輸出串流轉換為 .NET for Windows Store 應用程式中的受管理串流,並使用指定的緩衝區大小。

AsStreamForWrite(IOutputStream)

將 Windows 執行階段 中的輸出串流轉換為 .NET 中的管理串流,供 Windows 8.x Store 應用程式使用。

適用於