WindowsRuntimeStorageExtensions WindowsRuntimeStorageExtensions WindowsRuntimeStorageExtensions WindowsRuntimeStorageExtensions Class

Definizione

Contiene metodi di estensione per le interfacce IStorageFile e IStorageFolder in Windows Runtime disponibili durante lo sviluppo di app di Windows Store.Contains extension methods for the IStorageFile and IStorageFolder interfaces in the Windows Runtime when developing Windows Store apps.

public ref class WindowsRuntimeStorageExtensions abstract sealed
[System.Security.SecurityCritical]
public static class WindowsRuntimeStorageExtensions
type WindowsRuntimeStorageExtensions = class
Public Module WindowsRuntimeStorageExtensions
Ereditarietà
WindowsRuntimeStorageExtensionsWindowsRuntimeStorageExtensionsWindowsRuntimeStorageExtensionsWindowsRuntimeStorageExtensions
Attributi

Esempi

Nell'esempio seguente viene illustrato come aprire un file nei dati dell'applicazione come Stream in un'app StreamWriter di Windows Store e scrivervi utilizzando un'istanza della classe.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. Quindi legge il contenuto del file usando un'istanza della StreamReader classe.It then reads the contents for the file by using an instance of the StreamReader 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();
            }
        }
    }
}
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

Nell'esempio seguente viene illustrato il codice XAML associato all'esempio precedente.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>

Commenti

Questi metodi di estensione sono disponibili solo quando si sviluppano applicazioni Windows Store.These extension methods are available only when you develop Windows Store apps. I metodi offrono pratici modi per aprire i file per la lettura o la scrittura nelle app di Windows Store.The methods provide convenient ways of opening files for reading or writing in Windows Store apps. Non è possibile creare un'istanza della WindowsRuntimeStorageExtensions classe, bensì usare questi metodi dalle istanze IStorageFile delle interfacce e IStorageFolder .You do not create an instance of the WindowsRuntimeStorageExtensions class; instead, you use these methods from instances of the IStorageFile and IStorageFolder interfaces.

La WindowsRuntimeStorageExtensions classe contiene due metodi che si IStorageFile estendono per la lettura o la scrittura:The WindowsRuntimeStorageExtensions class contains two methods that extend IStorageFile for reading or writing:

La classe WindowsRuntimeStorageExtensions contiene due metodi che si IStorageFolder estendono per la lettura e la scrittura:The WindowsRuntimeStorageExtensions class contains two methods that extend IStorageFolder for reading and writing:

Metodi

CreateSafeFileHandle(IStorageFolder, String, FileMode) CreateSafeFileHandle(IStorageFolder, String, FileMode) CreateSafeFileHandle(IStorageFolder, String, FileMode) CreateSafeFileHandle(IStorageFolder, String, FileMode)

Crea un handle di file sicuro per un file che si trova nell'istanza corrente della cartella di archiviazione.Creates a safe file handle for a file that is in the current storage folder instance.

CreateSafeFileHandle(IStorageFile, FileAccess, FileShare, FileOptions) CreateSafeFileHandle(IStorageFile, FileAccess, FileShare, FileOptions) CreateSafeFileHandle(IStorageFile, FileAccess, FileShare, FileOptions) CreateSafeFileHandle(IStorageFile, FileAccess, FileShare, FileOptions)

Crea un handle di file sicuro per un'istanza del file di archiviazione corrente.Creates a safe file handle for a the current storage file instance.

CreateSafeFileHandle(IStorageFolder, String, FileMode, FileAccess, FileShare, FileOptions) CreateSafeFileHandle(IStorageFolder, String, FileMode, FileAccess, FileShare, FileOptions) CreateSafeFileHandle(IStorageFolder, String, FileMode, FileAccess, FileShare, FileOptions) CreateSafeFileHandle(IStorageFolder, String, FileMode, FileAccess, FileShare, FileOptions)

Crea un handle di file sicuro per un file che si trova nell'istanza corrente della cartella di archiviazione.Creates a safe file handle for a file that is in the current storage folder instance.

OpenStreamForReadAsync(IStorageFile) OpenStreamForReadAsync(IStorageFile) OpenStreamForReadAsync(IStorageFile) OpenStreamForReadAsync(IStorageFile)

Recupera un flusso per la lettura da un file specificato.Retrieves a stream for reading from a specified file.

OpenStreamForReadAsync(IStorageFolder, String) OpenStreamForReadAsync(IStorageFolder, String) OpenStreamForReadAsync(IStorageFolder, String) OpenStreamForReadAsync(IStorageFolder, String)

Recupera un flusso per la lettura da un file nella cartella padre specificata.Retrieves a stream for reading from a file in the specified parent folder.

OpenStreamForWriteAsync(IStorageFile) OpenStreamForWriteAsync(IStorageFile) OpenStreamForWriteAsync(IStorageFile) OpenStreamForWriteAsync(IStorageFile)

Recupera un flusso per la scrittura in un file specificato.Retrieves a stream for writing to a specified file.

OpenStreamForWriteAsync(IStorageFolder, String, CreationCollisionOption) OpenStreamForWriteAsync(IStorageFolder, String, CreationCollisionOption) OpenStreamForWriteAsync(IStorageFolder, String, CreationCollisionOption) OpenStreamForWriteAsync(IStorageFolder, String, CreationCollisionOption)

Recupera un flusso per la scrittura in un file nella cartella padre specificata.Retrieves a stream for writing to a file in the specified parent folder.

Si applica a