Export (0) Print
Expand All

WindowsRuntimeStorageExtensions.OpenStreamForReadAsync Method (IStorageFile)

.NET Framework 4.5

Retrieves a stream for reading from a specified file.

This API is not CLS-compliant. 

Namespace:  System.IO
Assembly:  System.Runtime.WindowsRuntime (in System.Runtime.WindowsRuntime.dll)

'Declaration
<ExtensionAttribute> _
<CLSCompliantAttribute(False)> _
Public Shared Function OpenStreamForReadAsync ( _
	windowsRuntimeFile As IStorageFile _
) As Task(Of Stream)

Parameters

windowsRuntimeFile
Type: IStorageFile

The Windows Runtime IStorageFile object to read from.

Return Value

Type: System.Threading.Tasks.Task(Of Stream)
A task that represents the asynchronous read operation.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IStorageFile. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

ExceptionCondition
ArgumentNullException

windowsRuntimeFile is Nothing.

IOException

The file could not be opened or retrieved as a stream.

The following example shows how to open a file as a Stream in a Windows Store app, and read its contents by using an instance of the StreamReader class.

Imports System.Text
Imports System.IO
Imports Windows.Storage.Pickers
Imports Windows.Storage

NotInheritable Public Class BlankPage
    Inherits Page



    Private Async Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
        Dim contents As StringBuilder = New StringBuilder()
        Dim nextLine As String 
        Dim lineCounter As Integer = 1

        Dim openPicker = New FileOpenPicker()
        openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary

        openPicker.FileTypeFilter.Add(".txt")
        Dim selectedFile As StorageFile = Await openPicker.PickSingleFileAsync()

        Using reader As StreamReader = New StreamReader(Await selectedFile.OpenStreamForReadAsync())
            nextLine = Await reader.ReadLineAsync()
            While (nextLine <> Nothing)
                contents.AppendFormat("{0}. ", lineCounter)
                contents.Append(nextLine)
                contents.AppendLine()
                lineCounter = lineCounter + 1
                If (lineCounter > 3) Then
                    contents.AppendLine("Only first 3 lines shown.")
                    Exit While 
                End If
                nextLine = Await reader.ReadLineAsync()
            End While 
        End Using
        DisplayContentsBlock.Text = contents.ToString()
    End Sub 
End Class

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="Display lines from a file."></TextBlock>
        <Button Content="Load File" Click="Button_Click_1"></Button>
        <TextBlock Name="DisplayContentsBlock"></TextBlock>
    </StackPanel>
</Page>

.NET Framework

Supported in: 4.6, 4.5

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone Silverlight 8

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.

Show:
© 2014 Microsoft