WindowsRuntimeStorageExtensions.OpenStreamForWriteAsync Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Överlagringar
| Name | Description |
|---|---|
| OpenStreamForWriteAsync(IStorageFile) |
Hämtar en dataström för att skriva till en angiven fil. |
| OpenStreamForWriteAsync(IStorageFolder, String, CreationCollisionOption) |
Hämtar en dataström för att skriva till en fil i den angivna överordnade mappen. |
OpenStreamForWriteAsync(IStorageFile)
Viktigt!
Detta API uppfyller inte CLS.
Hämtar en dataström för att skriva till en angiven fil.
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)
Parametrar
- windowsRuntimeFile
- Windows.Storage.IStorageFile
Objektet Windows Runtime Windows.Storage.IStorageFile att skriva till.
Returer
En uppgift som representerar den asynkrona skrivåtgärden.
- Attribut
Undantag
windowsRuntimeFile är null.
Det gick inte att öppna eller hämta filen som en dataström.
Exempel
I följande exempel visas hur du öppnar en fil som en Stream i en Windows Store-app och skriver till den med hjälp av en instans av klassen 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
I nästa exempel visas XAML-koden som är associerad med föregående exempel.
<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>
Kommentarer
Note
I Visual Basic och C# kan du anropa den här metoden som en instansmetod för alla objekt av typen <xref:Windows.Storage.IStorageFile>. När du använder instansmetodens syntax för att anropa den här metoden utelämnar du den första parametern. Mer information finns i Extension Methods (Visual Basic) eller Extension Methods (C# Programming Guide).
Gäller för
OpenStreamForWriteAsync(IStorageFolder, String, CreationCollisionOption)
Viktigt!
Detta API uppfyller inte CLS.
Hämtar en dataström för att skriva till en fil i den angivna överordnade mappen.
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)
Parametrar
- rootDirectory
- Windows.Storage.IStorageFolder
Objektet Windows Runtime Windows.Storage.IStorageFile som innehåller filen att skriva till.
- relativePath
- String
Sökvägen, i förhållande till rotmappen, till filen som du vill skriva till.
- creationCollisionOption
- Windows.Storage.CreationCollisionOption
Uppräkningsvärdet Windows Runtime Windows.Storage.CreationCollisionOption som anger det beteende som ska användas när namnet på filen som ska skapas är detsamma som namnet på en befintlig fil.
Returer
En uppgift som representerar den asynkrona skrivåtgärden.
- Attribut
Undantag
rootDirectory eller relativePath är null.
relativePath är tom eller innehåller endast blankstegstecken.
Det gick inte att öppna eller hämta filen som en dataström.
Exempel
I följande exempel visas hur du öppnar en fil som en Stream i en Windows Store-app och skriver till den med hjälp av en instans av klassen 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
I nästa exempel visas XAML-koden som är associerad med föregående exempel.
<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>
Kommentarer
Note
I Visual Basic och C# kan du anropa den här metoden som en instansmetod för alla objekt av typen <xref:Windows.Storage.IStorageFolder>. När du använder instansmetodens syntax för att anropa den här metoden utelämnar du den första parametern. Mer information finns i Extension Methods (Visual Basic) eller Extension Methods (C# Programming Guide).