次の方法で共有


Quickstart: Working with files and folders in Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

The StorageFolder class is used for reading and writing data to a file within the local folder. When you work with files, you also typically use the StreamReader class to read and write content to the file.

This topic contains the following sections.

Files and folders API overview

The following methods are typically used when working with StorageFolder:

StorageFolder..::.CreateFolderAsync

Creates a directory in the local folder.

StorageFolder..::.CreateFileAsync

Asynchronous method creates a file in the local folder.

OpenStreamForReadAsync

Retrieves a stream for reading a file in the specified folder.

OpenStreamForWriteAsync

Retrieves a stream for writing a file in the specified folder.

DeleteAsync

Deletes a folder.

RenameAsync

Renames a folder.

Files and folders example UI

The following image shows a sample app that demonstrates reading and writing text data to the local folder.

To create this UI, in the MainPage.xaml file, replace the Grid named ContentPanel with the following XAML.

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0">
    <TextBox 
        Name="textBox1" 
        HorizontalAlignment="Left"
        Height="72" 
        Margin="0,22,0,0" 
        TextWrapping="Wrap" 
        Text="Enter text here." 
        VerticalAlignment="Top" Width="456"/>
    <Button 
        Name='btnWrite' 
        Content="Write"
        HorizontalAlignment="Left" 
        Margin="10,94,0,0" 
        VerticalAlignment="Top"
        Width="156" 
        Click="btnWrite_Click"/>
    <TextBlock 
        Name="textBlock1" 
        HorizontalAlignment="Left"
        Margin="10,293,0,0" 
        TextWrapping="Wrap" Text=""
        VerticalAlignment="Top" 
        Height="61" 
        Width="436"/>
    <Button 
        Name="btnRead" 
        Content="Read"
        HorizontalAlignment="Left" 
        Margin="10,374,0,0" 
        VerticalAlignment="Top"
        Width="156" 
        IsEnabled="False"
        Click="btnRead_Click"/>
</Grid>

Files and folders directives

To work with files and folders using the code in this quickstart, add the following directives to projects created from the Windows Phone App template. Place these using statements at the top of the MainPage.xaml.cs file.

using System.IO;
using System.Threading.Tasks;
using Windows.Storage;

Creating a folder and writing to a text file

The following code shows how to create a folder and write to a text file. A file named DataFile.txt and folder named DataFolder are created if either of them don’t already exist. The file and folder are created with the CreateFileAsync and CreateFolderAsync methods. The CreationCollisionOption enumeration is used to specify what to do if the file or folder already exist.

private async void btnWrite_Click(object sender, RoutedEventArgs e)
{
    await WriteToFile();

    // Update UI.
    this.btnWrite.IsEnabled = false;
    this.btnRead.IsEnabled = true;
}

private async Task WriteToFile()
{
    // Get the text data from the textbox. 
    byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(this.textBox1.Text.ToCharArray());

    // Get the local folder.
    StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;

    // Create a new folder name DataFolder.
    var dataFolder = await local.CreateFolderAsync("DataFolder",
        CreationCollisionOption.OpenIfExists);

    // Create a new file named DataFile.txt.
    var file = await dataFolder.CreateFileAsync("DataFile.txt",
    CreationCollisionOption.ReplaceExisting);

    // Write the data from the textbox.
    using (var s = await file.OpenStreamForWriteAsync())
    {
        s.Write(fileBytes, 0, fileBytes.Length);
    }
}

Reading a text file

The following code shows how to read the text data.

private async void btnRead_Click(object sender, RoutedEventArgs e)
{
    await ReadFile();

    // Update UI.
    this.btnWrite.IsEnabled = true;
    this.btnRead.IsEnabled = false;
}

private async Task ReadFile()
{
    // Get the local folder.
    StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;

    if (local != null)
    {
        // Get the DataFolder folder.
        var dataFolder = await local.GetFolderAsync("DataFolder");

        // Get the file.
        var file = await dataFolder.OpenStreamForReadAsync("DataFile.txt");

        // Read the data.
        using (StreamReader streamReader = new StreamReader(file))
        {
            this.textBlock1.Text = streamReader.ReadToEnd();
        }

    }
}

See Also

Other Resources

Quickstart: Working with files and folders in Windows Phone 7