WindowsRuntimeStorageExtensions.OpenStreamForWriteAsync 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| OpenStreamForWriteAsync(IStorageFile) |
擷取串流以寫入指定檔案。 |
| OpenStreamForWriteAsync(IStorageFolder, String, CreationCollisionOption) |
擷取串流,用於寫入指定父資料夾中的檔案。 |
OpenStreamForWriteAsync(IStorageFile)
重要
此 API 不符合 CLS 規範。
擷取串流以寫入指定檔案。
public:
[System::Runtime::CompilerServices::Extension]
static System::Threading::Tasks::Task<System::IO::Stream ^> ^ OpenStreamForWriteAsync(Windows::Storage::IStorageFile ^ windowsRuntimeFile);
[System.CLSCompliant(false)]
public static System.Threading.Tasks.Task<System.IO.Stream> OpenStreamForWriteAsync(this Windows.Storage.IStorageFile windowsRuntimeFile);
[<System.CLSCompliant(false)>]
static member OpenStreamForWriteAsync : Windows.Storage.IStorageFile -> System.Threading.Tasks.Task<System.IO.Stream>
<Extension()>
Public Function OpenStreamForWriteAsync (windowsRuntimeFile As IStorageFile) As Task(Of Stream)
參數
- windowsRuntimeFile
- Windows.Storage.IStorageFile
Windows 執行階段 Windows.Storage.IStorageFile 物件要寫入。
傳回
一個代表非同步寫入操作的任務。
- 屬性
例外狀況
windowsRuntimeFile 是 null。
該檔案無法以串流方式開啟或取回。
範例
以下範例展示了如何在 Windows Store 應用程式中以 Stream 開啟檔案,並透過 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)
{
StorageFile newFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("testfile3.txt");
using (StreamWriter writer = new StreamWriter(await newFile.OpenStreamForWriteAsync()))
{
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("testfile3.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)
Dim newFile As StorageFile = Await ApplicationData.Current.LocalFolder.CreateFileAsync("testfile4.txt")
Using writer As StreamWriter = New StreamWriter(Await newFile.OpenStreamForWriteAsync())
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("testfile4.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>
備註
Note
在 Visual Basic 和 C# 中,你可以將此方法作為實例方法呼叫,適用於任何類型為 <xref:Windows.Storage.IStorageFile> 的物件。 當你使用 instance method 語法呼叫這個方法時,請省略第一個參數。 欲了解更多資訊,請參閱 Extension Methods (Visual Basic) 或 Extension Methods (C# 程式設計指南)。
適用於
OpenStreamForWriteAsync(IStorageFolder, String, CreationCollisionOption)
重要
此 API 不符合 CLS 規範。
擷取串流,用於寫入指定父資料夾中的檔案。
public:
[System::Runtime::CompilerServices::Extension]
static System::Threading::Tasks::Task<System::IO::Stream ^> ^ OpenStreamForWriteAsync(Windows::Storage::IStorageFolder ^ rootDirectory, System::String ^ relativePath, Windows::Storage::CreationCollisionOption creationCollisionOption);
[System.CLSCompliant(false)]
public static System.Threading.Tasks.Task<System.IO.Stream> OpenStreamForWriteAsync(this Windows.Storage.IStorageFolder rootDirectory, string relativePath, Windows.Storage.CreationCollisionOption creationCollisionOption);
[<System.CLSCompliant(false)>]
static member OpenStreamForWriteAsync : Windows.Storage.IStorageFolder * string * Windows.Storage.CreationCollisionOption -> System.Threading.Tasks.Task<System.IO.Stream>
<Extension()>
Public Function OpenStreamForWriteAsync (rootDirectory As IStorageFolder, relativePath As String, creationCollisionOption As CreationCollisionOption) As Task(Of Stream)
參數
- rootDirectory
- Windows.Storage.IStorageFolder
Windows 執行階段 Windows.Storage.IStorageFile 物件,包含要寫入的檔案。
- relativePath
- String
相對於根資料夾的路徑,指向要寫入的檔案。
- creationCollisionOption
- Windows.Storage.CreationCollisionOption
當要建立的檔案名稱與現有檔案名稱相同時,Windows 執行階段 Windows.Storage.CreationCollisionOption 枚舉值,該值指定應使用的行為。
傳回
一個代表非同步寫入操作的任務。
- 屬性
例外狀況
rootDirectory 或 relativePath 為 null。
relativePath 是空的或僅包含空白字元。
該檔案無法以串流方式開啟或取回。
範例
以下範例展示了如何在 Windows Store 應用程式中以 Stream 開啟檔案,並透過 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>
備註
Note
在 Visual Basic 和 C# 中,你可以將此方法作為實例方法呼叫,適用於任何類型為 <xref:Windows.Storage.IStorageFolder> 的物件。 當你使用 instance method 語法呼叫這個方法時,請省略第一個參數。 欲了解更多資訊,請參閱 Extension Methods (Visual Basic) 或 Extension Methods (C# 程式設計指南)。