Freigeben über


Quickstart: Working with files and folders in Windows Phone 7

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

The IsolatedStorageFile class is used for saving data to a file in the app’s local folder. When you work with files, you also typically use the IsolatedStorageFileStream class to read and write content to the file.

Note

If you’re developing a Windows Phone 8 app, see Quickstart: Working with files and folders in Windows Phone 8.

This topic contains the following sections.

Files and folders API overview

The following methods are typically used when working with IsolatedStorageFile:

GetUserStoreForApplication

Obtains the local folder.

FileExists

Determines whether the specified path refers to an existing file in the local folder.

CreateFile

Creates a file in the local folder.

OpenFile

Opens a file in the local folder at a specified path by using the specified sharing and access options. This method returns an IsolatedStorageFileStream object that contains the file's stream.

DeleteFile

Deletes a file in the local folder.

DirectoryExists

Determines whether the specified path refers to an existing directory in the local folder.

CreateDirectory

Creates a directory in the local folder.

DeleteDirectory

Deletes a directory in the local folder.

Remove

Removes the scope of the IsolatedStorageFile object and all its contents.

Files and folders example UI

The following image shows a sample app that writes text to, and reads text from 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>

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. An IsolatedStorageFileStream is created to write the data.

private void btnWrite_Click(object sender, RoutedEventArgs e)
{
    // Get the local folder.
    System.IO.IsolatedStorage.IsolatedStorageFile local =
        System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();

    // Create a new folder named DataFolder.
    if (!local.DirectoryExists("DataFolder"))
        local.CreateDirectory("DataFolder");

    // Create a new file named DataFile.txt.
    using (var isoFileStream = 
            new System.IO.IsolatedStorage.IsolatedStorageFileStream(
                "DataFolder\\DataFile.txt", 
                System.IO.FileMode.OpenOrCreate,
                    local))
    {
        // Write the data from the textbox.
        using (var isoFileWriter = new System.IO.StreamWriter(isoFileStream))
        {
            isoFileWriter.WriteLine(this.textBox1.Text);
        }
    }

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

Instances of IsolatedStorageFile and IsolatedStorageFileStream should be disposed of after their use. The using statement does this for you automatically and its use is considered a best practice.

Reading a text file

The following code shows how to read the text data to the IsolatedStorageFileStream.

private void btnRead_Click(object sender, RoutedEventArgs e)
{
    // Obtain a virtual store for the application.
    System.IO.IsolatedStorage.IsolatedStorageFile local =
        System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();

    // Specify the file path and options.
    using (var isoFileStream =
            new System.IO.IsolatedStorage.IsolatedStorageFileStream
                ("DataFolder\\DataFile.txt", System.IO.FileMode.Open, local))
    {
        // Read the data.
        using (var isoFileReader = new System.IO.StreamReader(isoFileStream))
        {
            this.textBlock1.Text = isoFileReader.ReadLine();
        }
    }
    // Update UI.
    this.btnWrite.IsEnabled = true;
    this.btnRead.IsEnabled = false;
}

See Also

Other Resources

Quickstart: Working with files and folders in Windows Phone 8