How to: Store Files and Folders for Windows Phone
March 23, 2012
This topic shows you how to perform the following isolated storage tasks in your application:
Obtain a virtual store for an application
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 application in which you can enter a string of text, write it to a file, and then read the contents of the text file.
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. |
To begin, you need to create a new project and add the necessary isolated storage namespaces.
To create a new project and add namespaces
-
In Visual Studio 2010 Express for Windows Phone, create a new project by selecting the File | New Project menu command.
-
The New Project window is displayed. Expand the Visual C# templates, and then select the Silverlight for Windows Phone templates.
-
Select the Windows Phone Application template. Fill in the Name with a name of your choice.
-
Click OK. The New Windows Phone Application window is displayed.
-
In the Target Windows Phone Version menu, ensure that Windows Phone 7.1 is selected.
-
Click OK. A new project is created, and MainPage.xaml is opened in the Visual Studio designer window.
-
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 application title if you want to.
-
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.
To add controls for the project
-
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.
-
Rename the TextBox to txtWrite and rename the Button to btnWrite.
-
For the TextBox, remove the text TextBox from the control Text property, and change the button text to Write in the content property.
-
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.
-
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.
In this section, you add code to the btnWrite and btnRead click event handlers to enable your application 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
-
Double-click the btnWrite button to add an event handler for the click event. The MainPage.xaml.cs file opens.
-
The objective is to add code to obtain a virtual store where you can create files and folders for your application. 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); } } }
-
Double-click the btnRead button to add an event handler for the click event. The MainPage.xaml.cs file opens.
-
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."; } }
-
Run the application by selecting the Debug menu and then click Start Debugging. This opens the emulator window and launches the application.
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 application, creating a new file, and then writing to and reading the contents of that file.
When running the application on the device, quit the application and then relaunch it. Pressing the Read button displays the last value you wrote from the previous time that you ran the application.
|
|
Note: