WindowsRuntimeStorageExtensions 类

定义

在开发 Windows 应用商店应用程序时,将 IStorageFileIStorageFolder 接口的扩展方法包含在 Windows 运行时中。

public ref class WindowsRuntimeStorageExtensions abstract sealed
[System.Security.SecurityCritical]
public static class WindowsRuntimeStorageExtensions
[<System.Security.SecurityCritical>]
type WindowsRuntimeStorageExtensions = class
Public Module WindowsRuntimeStorageExtensions
继承
WindowsRuntimeStorageExtensions
属性

示例

以下示例演示如何在 Windows 应用商店应用中将应用程序数据中的文件作为 Stream 打开,并使用 类的 StreamWriter 实例写入该文件。 然后,它通过使用 类的 StreamReader 实例读取文件的内容。

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)
        {
            using (StreamWriter writer =
                new StreamWriter(await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(
                "testfile.txt",  CreationCollisionOption.OpenIfExists)))
            {
                await writer.WriteLineAsync("new entry");
                await writer.WriteLineAsync(UserText.Text);
            }
        }

        private async void VerifyButton_Click(object sender, RoutedEventArgs e)
        {
            StorageFile openedFile = await ApplicationData.Current.LocalFolder.GetFileAsync("testfile.txt");
            using (StreamReader reader = new StreamReader(await openedFile.OpenStreamForReadAsync()))
            {
                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)
        Using writer As StreamWriter =
               New StreamWriter(Await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(
               "testfile.txt", CreationCollisionOption.OpenIfExists))
            Await writer.WriteLineAsync("new entry")
            Await writer.WriteLineAsync(UserText.Text)
        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")
        Using reader As StreamReader = New StreamReader(Await openedFile.OpenStreamForReadAsync())
            Results.Text = Await reader.ReadToEndAsync()
        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 应用商店应用中打开文件以读取或写入的便捷方法。 不创建 类的WindowsRuntimeStorageExtensions实例;而是从 和 IStorageFolder 接口的IStorageFile实例使用这些方法。

WindowsRuntimeStorageExtensions 包含两个扩展 IStorageFile 用于读取或写入的方法:

WindowsRuntimeStorageExtensions 类包含两种扩展 IStorageFolder 用于读取和写入的方法:

方法

OpenStreamForReadAsync(IStorageFile)

检索流以从指定的文件中读取。

OpenStreamForReadAsync(IStorageFolder, String)

检索流以从指定的父文件夹的一个文件中读取。

OpenStreamForWriteAsync(IStorageFile)

检索流以写入指定的文件。

OpenStreamForWriteAsync(IStorageFolder, String, CreationCollisionOption)

检索流以在指定的父文件夹中写入文件。

适用于