Skip to main content
.NET Framework Class Library
WindowsRuntimeStorageExtensions Class

Contains extension methods for the IStorageFile and IStorageFolder interfaces in the Windows Runtime when developing Windows Store apps.

Inheritance Hierarchy
SystemObject
  System.IOWindowsRuntimeStorageExtensions

Namespace:   System.IO
Assembly:  System.Runtime.WindowsRuntime (in System.Runtime.WindowsRuntime.dll)
Syntax
<[%$TOPIC/hh582101_en-us_VS_110_4_0_0_0_0%]> _
Public NotInheritable Class WindowsRuntimeStorageExtensions
public static class WindowsRuntimeStorageExtensions
[[%$TOPIC/hh582101_en-us_VS_110_4_0_2_0_0%]]
public ref class WindowsRuntimeStorageExtensions abstract sealed
[<[%$TOPIC/hh582101_en-us_VS_110_4_0_3_0_0%]>]
[<[%$TOPIC/hh582101_en-us_VS_110_4_0_3_0_1%]>]
type WindowsRuntimeStorageExtensions =  class end
Methods
  NameDescription
Public method Static member Supported in .NET for Windows Store apps OpenStreamForReadAsync(IStorageFile)Retrieves a stream for reading from a specified file.
Public method Static member Supported in .NET for Windows Store apps OpenStreamForReadAsync(IStorageFolder, String)Retrieves a stream for reading from a file in the specified parent folder.
Public method Static member Supported in .NET for Windows Store apps OpenStreamForWriteAsync(IStorageFile)Retrieves a stream for writing to a specified file.
Public method Static member Supported in .NET for Windows Store apps OpenStreamForWriteAsync(IStorageFolder, String, CreationCollisionOption)Retrieves a stream for writing to a file in the specified parent folder.
Top
Remarks

These extension methods are available only when you develop Windows Store apps. The methods provide convenient ways of opening files for reading or writing in Windows Store apps. You do not create an instance of the WindowsRuntimeStorageExtensions class; instead, you use these methods from instances of the IStorageFile and IStorageFolder interfaces.

The WindowsRuntimeStorageExtensions class contains two methods that extend IStorageFile for reading or writing:

The WindowsRuntimeStorageExtensions class contains two methods that extend IStorageFolder for reading and writing:

Examples

The following example shows how to open a file in the application data as a Stream in a Windows Store app, and write to it by using an instance of the StreamWriter class. It then reads the contents for the file by using an instance of the StreamReader class.

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
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();
            }
        }
    }
}

The next example shows the XAML code that is associated with the previous example.

<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>
Version Information

.NET Framework

Supported in: 4.5

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Silverlight 8.1
Platforms

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.