WindowsRuntimeStorageExtensions 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
包含開發 Windows Store 應用程式時,Windows 執行階段 中 Windows.Storage.IStorageFile 與 Windows.Storage.IStorageFolder 介面的擴充方法。
public ref class WindowsRuntimeStorageExtensions abstract sealed
[System.Security.SecurityCritical]
public static class WindowsRuntimeStorageExtensions
[<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;而是從 和 <xref:Windows.Storage.IStorageFile> 介面的實例<xref:Windows.Storage.IStorageFolder>中使用這些方法。
該 WindowsRuntimeStorageExtensions 類別包含兩種可擴展 <xref:Windows.Storage.IStorageFile> 用於讀取或寫入的方法:
WindowsRuntimeStorageExtensions 類別包含兩種可擴充 <xref:Windows.Storage.IStorageFolder> 讀寫的方法:
- OpenStreamForReadAsync(IStorageFolder, String)
- OpenStreamForWriteAsync(IStorageFolder, String, CreationCollisionOption)
方法
| 名稱 | Description |
|---|---|
| OpenStreamForReadAsync(IStorageFile) |
從指定檔案讀取串流。 |
| OpenStreamForReadAsync(IStorageFolder, String) |
從指定的父資料夾中讀取串流以讀取。 |
| OpenStreamForWriteAsync(IStorageFile) |
擷取串流以寫入指定檔案。 |
| OpenStreamForWriteAsync(IStorageFolder, String, CreationCollisionOption) |
擷取串流,用於寫入指定父資料夾中的檔案。 |