Windows apps
Collapse the table of content
Expand the table of content

How to store files and folders for Windows Phone

[This topic contains pre-release information and is subject to change. Blank topics are included as placeholders.]

September 25, 2012

Applies to: Windows Phone 8 | Windows Phone OS 7.1

 

This topic shows you how to perform the following isolated storage tasks in your app:

  • Obtain a virtual store for an app

  • Create a parent folder

  • Create and add text to an isolated storage file

  • Read the text placed in the storage file

The objective is to create a single-page app in which you can enter a string of text, write it to a file, and then read the contents of the text file.

Note Note:

The steps in the following procedure are for Visual Studio 2010 Express for Windows Phone. You may see some minor variations in menu commands or window layouts when you are using the add-in for Visual Studio 2010 Professional or Visual Studio 2010 Ultimate.  

This topic contains the following sections.

 

To begin, you need to create a new project and add the necessary isolated storage namespaces.

To create a new project and add namespaces

  1. In Visual Studio 2010 Express for Windows Phone, create a new project by selecting the File | New Project menu command.

  2. The New Project window is displayed. Expand the Visual C# templates, and then select the Windows Phone templates.

  3. Select the Windows Phone App  template. Fill in the Name with a name of your choice.

  4. Click OK. The Windows Phone platform selection dialog box is displayed.

  5. In the Target Windows Phone Version menu, ensure that Windows Phone OS 7.1 is selected.

  6. Click OK. A new project is created, and MainPage.xaml is opened in the Visual Studio designer window.

  7. From the designer view on MainPage.xaml, select page title and change the title text to files/folders in the Text property or directly in the XAML. You can also rename the app title if you want to.

  8. Add the following resources in the code-behind page to include the following namespaces. For example, if you are using the main page with the default naming convention, you would update MainPage.xaml.cs.

    using System.IO;
    using System.IO.IsolatedStorage;
    
    

To add controls for the project

  1. On MainPage.xaml, drag a TextBox and a Button control from the Toolbox and place the controls near the top of the page. Scale and position the controls by using the following illustration as a reference.

  2. Rename the TextBox to txtWrite and rename the Button to btnWrite.

  3. For the TextBox, remove the text TextBox from the control Text property, and change the button text to Write in the content property.

  4. On MainPage.xaml, drag a TextBlock and a Button control from the Toolbox and place the controls near the bottom of the page. Scale and position the controls by using the illustration below as a reference.

  5. Rename the TextBlock to txtRead and rename the Button to btnRead. For the TextBlock, remove the text TextBlock from the control Text property, and change the button text to Read in the content property.

    AP_Core_IsoDesign

In this section, you add code to the btnWrite and btnRead click event handlers to enable your app to create a folder structure and a file, and to read and write text to the file.

To add code to the button event handlers

  1. Double-click the btnWrite button to add an event handler for the click event. The MainPage.xaml.cs file opens.

  2. The objective is to add code to obtain a virtual store where you can create files and folders for your app. To highlight a simple file and folder hierarchy, you create a folder and then create a text file inside that folder. For the btnWrite_Click event handler, add the following code.

    private void btnWrite_Click(object sender, RoutedEventArgs e)
    {
        // Obtain the virtual store for the application.
        IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();
    
        // Create a new folder and call it "MyFolder".
        myStore.CreateDirectory("MyFolder");
    
        // Specify the file path and options.
        using (var isoFileStream = new IsolatedStorageFileStream("MyFolder\\myFile.txt", FileMode.OpenOrCreate, myStore))
        {
            //Write the data
            using (var isoFileWriter = new StreamWriter(isoFileStream))
            {
                isoFileWriter.WriteLine(txtWrite.Text);
            }
        }
    }
    
    
  3. Double-click the btnRead button to add an event handler for the click event. The MainPage.xaml.cs file opens.

  4. For the btnRead_Click event handler, add the following code.

    // This code opens and reads the contents of myFile.txt.
    private void btnRead_Click(object sender, RoutedEventArgs e)
    {
        // Obtain a virtual store for the application.
        IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();
    
        try
        {
            // Specify the file path and options.
            using (var isoFileStream = new IsolatedStorageFileStream("MyFolder\\myFile.txt", FileMode.Open, myStore))
            {
                // Read the data.
                using (var isoFileReader = new StreamReader(isoFileStream))
                {
                    txtRead.Text = isoFileReader.ReadLine();
                }
            }
    
        }
        catch
        {
            // Handle the case when the user attempts to click the Read button first.
            txtRead.Text = "Need to create directory and the file first.";
        }
    }
    
    
  5. Run the app by selecting the Debug menu and then click Start Debugging. This opens the emulator window and launches the app.

You should be able to enter text into the TextBox control, tap Write, next tap Read, and have the contents display in the TextBlock control. Again, this shows the process of creating space in isolated storage for your app, creating a new file, and then writing to and reading the contents of that file.

When running the app on the device, quit the app and then relaunch it. Pressing the Read button displays the last value you wrote from the previous time that you ran the app.

AP_Core_IsoWrite

AP_Core_IsoRead

Show:
© 2016 Microsoft