共用方式為


WindowsRuntimeStorageExtensions.OpenStreamForReadAsync 方法

定義

多載

OpenStreamForReadAsync(IStorageFile)

從指定檔案擷取資料流進行讀取。

OpenStreamForReadAsync(IStorageFolder, String)

從指定上層資料夾中的檔案中擷取資料流進行讀取。

OpenStreamForReadAsync(IStorageFile)

重要

此 API 不符合 CLS 規範。

從指定檔案擷取資料流進行讀取。

public:
[System::Runtime::CompilerServices::Extension]
 static System::Threading::Tasks::Task<System::IO::Stream ^> ^ OpenStreamForReadAsync(Windows::Storage::IStorageFile ^ windowsRuntimeFile);
[System.CLSCompliant(false)]
public static System.Threading.Tasks.Task<System.IO.Stream> OpenStreamForReadAsync (this Windows.Storage.IStorageFile windowsRuntimeFile);
[<System.CLSCompliant(false)>]
static member OpenStreamForReadAsync : Windows.Storage.IStorageFile -> System.Threading.Tasks.Task<System.IO.Stream>
<Extension()>
Public Function OpenStreamForReadAsync (windowsRuntimeFile As IStorageFile) As Task(Of Stream)

參數

windowsRuntimeFile
IStorageFile

要從中讀取的 Windows 執行階段 IStorageFile 物件。

傳回

表示非同步讀取作業的工作。

屬性

例外狀況

windowsRuntimeFilenull

檔案無法開啟,或無法擷取成資料流。

範例

下列範例示範如何在 Windows 市集應用程式中開啟檔案做為 Stream ,並使用 類別的 StreamReader 實例來讀取其內容。

using System;
using System.IO;
using System.Text;
using Windows.Storage.Pickers;
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 Button_Click_1(object sender, RoutedEventArgs e)
        {
            StringBuilder contents = new StringBuilder();
            string nextLine;
            int lineCounter = 1;

            var openPicker = new FileOpenPicker();
            openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            openPicker.FileTypeFilter.Add(".txt");
            StorageFile selectedFile = await openPicker.PickSingleFileAsync();

            using (StreamReader reader = new StreamReader(await selectedFile.OpenStreamForReadAsync()))
            {
                while ((nextLine = await reader.ReadLineAsync()) != null)
                {
                    contents.AppendFormat("{0}. ", lineCounter);
                    contents.Append(nextLine);
                    contents.AppendLine();
                    lineCounter++;
                    if (lineCounter > 3)
                    {
                        contents.AppendLine("Only first 3 lines shown.");
                        break;
                    }
                }
            }
            DisplayContentsBlock.Text = contents.ToString();
        }
    }
}
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

下一個範例顯示與上一個範例相關聯的 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="Display lines from a file."></TextBlock>
        <Button Content="Load File" Click="Button_Click_1"></Button>
        <TextBlock Name="DisplayContentsBlock"></TextBlock>
    </StackPanel>
</Page>

備註

注意

在 Visual Basic 和 C# 中,您可以將這個方法呼叫為 類型 IStorageFile之任何對象的實例方法。 使用執行個體方法語法呼叫這個方法時,請省略第一個參數。 如需詳細資訊,請參閱 Visual Basic) 或擴充方法 ( (C# 程式設計指南中的擴充方法)

適用於

OpenStreamForReadAsync(IStorageFolder, String)

重要

此 API 不符合 CLS 規範。

從指定上層資料夾中的檔案中擷取資料流進行讀取。

public:
[System::Runtime::CompilerServices::Extension]
 static System::Threading::Tasks::Task<System::IO::Stream ^> ^ OpenStreamForReadAsync(Windows::Storage::IStorageFolder ^ rootDirectory, System::String ^ relativePath);
[System.CLSCompliant(false)]
public static System.Threading.Tasks.Task<System.IO.Stream> OpenStreamForReadAsync (this Windows.Storage.IStorageFolder rootDirectory, string relativePath);
[<System.CLSCompliant(false)>]
static member OpenStreamForReadAsync : Windows.Storage.IStorageFolder * string -> System.Threading.Tasks.Task<System.IO.Stream>
<Extension()>
Public Function OpenStreamForReadAsync (rootDirectory As IStorageFolder, relativePath As String) As Task(Of Stream)

參數

rootDirectory
IStorageFolder

包含做為讀取來源檔案的 Windows 執行階段 IStorageFile 物件。

relativePath
String

要讀取之檔案的路徑(相對於根資料夾)。

傳回

表示非同步讀取作業的工作。

屬性

例外狀況

rootDirectoryrelativePathnull

relativePath 是空的或僅包含空白字元。

檔案無法開啟,或無法擷取成資料流。

範例

下列範例示範如何在 Windows 市集應用程式中開啟檔案做為 Stream ,並使用 類別的 StreamReader 實例來讀取其內容。

using System;
using System.IO;
using Windows.Storage.Pickers;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace ExampleApplication
{
    public sealed partial class BlankPage : Page
    {
        public BlankPage()
        {
            this.InitializeComponent();
            CreateTestFile();
        }

        private async void CreateTestFile()
        {
            StorageFile newFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("testfile.txt");
            await FileIO.WriteTextAsync(newFile, "example content in file");
        }

        private async void Button_Click_1(object sender, RoutedEventArgs e)
        {
            using (StreamReader reader = new StreamReader(
                await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync("testfile.txt")))
            {
                string contents = await reader.ReadToEndAsync();
                DisplayContentsBlock.Text = contents;
            }
        }
    }
}
Imports System.IO
Imports Windows.Storage

NotInheritable Public Class BlankPage
    Inherits Page

    Protected Overrides Sub OnNavigatedTo(e As Navigation.NavigationEventArgs)
        CreateTestFile()
    End Sub

    Private Async Sub CreateTestFile()
        Dim newFile As StorageFile = Await ApplicationData.Current.LocalFolder.CreateFileAsync("testfile.txt")
        Await FileIO.WriteTextAsync(newFile, "example content in file")
    End Sub

    Private Async Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
        Using reader As StreamReader = New StreamReader(
                Await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync("testfile.txt"))

            Dim contents As String = Await reader.ReadToEndAsync()
            DisplayContentsBlock.Text = contents
        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">
        <Button Content="Open File" Click="Button_Click_1"></Button>
        <TextBlock Name="DisplayContentsBlock"></TextBlock>
    </StackPanel>
</Page>

備註

注意

在 Visual Basic 和 C# 中,您可以在 IStorageFolder 類型的任何物件上呼叫此方法作為實例方法。 使用執行個體方法語法呼叫這個方法時,請省略第一個參數。 如需詳細資訊,請參閱 Visual Basic) 或擴充方法 ( (C# 程式設計指南中的擴充方法)

適用於