WindowsRuntimeStorageExtensions 類別

定義

包含開發 Windows 市集應用程式時,用於 Windows 執行階段中 IStorageFileIStorageFolder 介面的擴充方法。

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 市集應用程式中將應用程式資料中的 檔案開啟為 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 以進行讀取和寫入:

方法

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)

擷取資料流以寫入指定上層資料夾中的檔案。

適用於