WindowsRuntimeStorageExtensions 类


在开发 Windows 应用商店应用程序时,将 IStorageFileIStorageFolder 接口的扩展方法包含在 Windows 运行时中。Contains extension methods for the IStorageFile and IStorageFolder interfaces in the Windows Runtime when developing Windows Store apps.

public ref class WindowsRuntimeStorageExtensions abstract sealed
public static class WindowsRuntimeStorageExtensions
type WindowsRuntimeStorageExtensions = class
Public Module WindowsRuntimeStorageExtensions


下面的示例演示如何在 Windows 应用商店应用程序中以的Stream形式打开应用程序数据中的文件, 并使用StreamWriter类的实例向其写入数据。The following example shows how to open a file in the application data as a Stream in a Windows Store app, and write to it by using an instance of the StreamWriter class. 然后, 它使用StreamReader类的实例来读取该文件的内容。It then reads the contents for the file by using an instance of the StreamReader class.

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

        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 代码。The next example shows the XAML code that is associated with the previous example.


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


仅当开发 Windows 应用商店应用时, 这些扩展方法才可用。These extension methods are available only when you develop Windows Store apps. 方法提供了在 Windows 应用商店应用中打开文件以进行读取或写入的便利方法。The methods provide convenient ways of opening files for reading or writing in Windows Store apps. 不创建WindowsRuntimeStorageExtensions类的实例, 而是使用IStorageFileIStorageFolder接口的实例中的这些方法。You do not create an instance of the WindowsRuntimeStorageExtensions class; instead, you use these methods from instances of the IStorageFile and IStorageFolder interfaces.

类包含两个扩展IStorageFile以进行读取或写入的方法: WindowsRuntimeStorageExtensionsThe WindowsRuntimeStorageExtensions class contains two methods that extend IStorageFile for reading or writing: 类包含两个扩展IStorageFolder以进行读取和写入的方法:The WindowsRuntimeStorageExtensions class contains two methods that extend IStorageFolder for reading and writing:


CreateSafeFileHandle(IStorageFile, FileAccess, FileShare, FileOptions)

为当前存储文件实例创建安全文件句柄。Creates a safe file handle for a the current storage file instance.

CreateSafeFileHandle(IStorageFolder, String, FileMode)

为当前存储文件夹实例中的文件创建安全文件句柄。Creates a safe file handle for a file that is in the current storage folder instance.

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

为当前存储文件夹实例中的文件创建安全文件句柄。Creates a safe file handle for a file that is in the current storage folder instance.


检索流以从指定的文件中读取。Retrieves a stream for reading from a specified file.

OpenStreamForReadAsync(IStorageFolder, String)

检索流以从指定的父文件夹的一个文件中读取。Retrieves a stream for reading from a file in the specified parent folder.


检索流以写入指定的文件。Retrieves a stream for writing to a specified file.

OpenStreamForWriteAsync(IStorageFolder, String, CreationCollisionOption)

检索流以在指定的父文件夹中写入文件。Retrieves a stream for writing to a file in the specified parent folder.