WindowsRuntimeStorageExtensions 类

定义

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

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

示例

以下示例演示如何在应用程序数据中以 Windows Store 应用中的形式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实例;而是从接口的IStorageFileIStorageFolder实例使用这些方法。

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

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

方法

CreateSafeFileHandle(IStorageFile, FileAccess, FileShare, FileOptions)

为当前存储文件实例创建安全文件句柄。

CreateSafeFileHandle(IStorageFolder, String, FileMode)

为当前存储文件夹实例中的文件创建安全文件句柄。

CreateSafeFileHandle(IStorageFolder, String, FileMode, FileAccess, FileShare, FileOptions)

为当前存储文件夹实例中的文件创建安全文件句柄。

OpenStreamForReadAsync(IStorageFile)

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

OpenStreamForReadAsync(IStorageFolder, String)

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

OpenStreamForWriteAsync(IStorageFile)

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

OpenStreamForWriteAsync(IStorageFolder, String, CreationCollisionOption)

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

适用于