WindowsRuntimeStorageExtensions.OpenStreamForWriteAsync 메서드

정의

오버로드

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
IStorageFile

쓸 Windows 런타임 IStorageFile 개체입니다.

반환

비동기 쓰기 작업을 나타내는 작업입니다.

특성

예외

windowsRuntimeFile이(가) null인 경우

파일을 열 수 없거나 스트림으로 검색할 수 없습니다.

예제

다음 예제에서는 Windows 스토어 앱에서 으로 Stream 파일을 열고 클래스의 StreamReader instance 사용하여 파일을 쓰는 방법을 보여 둡니다.

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>

설명

참고

Visual Basic 및 C#에서는 형식IStorageFile의 모든 개체에서 이 메서드를 instance 메서드로 호출할 수 있습니다. 인스턴스 메서드 구문을 사용하여 이 메서드를 호출할 경우에는 첫 번째 매개 변수를 생략합니다. 자세한 내용은 확장 메서드(Visual Basic) 또는 확장 메서드(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
IStorageFolder

쓸 파일이 포함된 Windows 런타임 IStorageFile 개체입니다.

relativePath
String

쓸 파일의 경로(루트 폴더에 상대적)입니다.

creationCollisionOption
CreationCollisionOption

만들 파일 이름이 기존 파일 이름과 같은 경우 사용할 동작을 지정하는 Windows 런타임 CreationCollisionOption 열거형 값입니다.

반환

비동기 쓰기 작업을 나타내는 작업입니다.

특성

예외

rootDirectory 또는 relativePathnull인 경우

relativePath이 비어 있거나 공백 문자만 있습니다.

파일을 열 수 없거나 스트림으로 검색할 수 없습니다.

예제

다음 예제에서는 Windows 스토어 앱에서 으로 Stream 파일을 열고 클래스의 StreamReader instance 사용하여 파일을 쓰는 방법을 보여 둡니다.

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>

설명

참고

Visual Basic 및 C#에서는 형식IStorageFolder의 모든 개체에서 이 메서드를 instance 메서드로 호출할 수 있습니다. 인스턴스 메서드 구문을 사용하여 이 메서드를 호출할 경우에는 첫 번째 매개 변수를 생략합니다. 자세한 내용은 확장 메서드(Visual Basic) 또는 확장 메서드(C# 프로그래밍 가이드)를 참조하세요.

적용 대상