Working with Files and Folders in WP7


Introduction

In this article we are going to see the most important topic on the Windows Phone 7 Development, which is the Isolated Storage for Files and Folders. Typically any application development requires a location to store the user data locally to manipulate, here with Windows Phone 7 development we have the Isolated Storage to store the data in a isolated location which is accessible to that particular application only. In our previous article we have seen what Isolated Storage is for Windows Phone 7 Development and also we have seen about how to use the dictionary to store data locally in key value pair.

In this tutorial we are going to see how to perform the below tasks on the Isolated Storage for Files and Folders.

  • Write some text to a text file
  • Read the text file
  • Delete the text file
  • Write some text to a text file inside a folder
  • Read the text file inside a folder

Let us jump start to see the step by step process on how to use the Files and Folders to store the data locally with Windows Phone 7 device.

Steps:

Open Visual Studio 2010 IDE as an administrator and create a new Silverlight for Windows Phone 7 project with a valid project name as shown in the screen below.

Clipboard01.jpg

IsolatedStorageFileStream class is used in Windows Phone 7 Development to read, write and create files in an Isolated Storage, we can create an instance of this class to get the stream of object to read and write using the StreamReader and StreamWriter. To start with let us first design a unique interface by using the controls from the tool box, once the design is completed we can see the screen as shown in the screen below. Just copy the XAML Code below to get the same look and feel.

Clipboard02.jpg

XAML Code

<Grid x:Name="LayoutRoot" Background="Transparent">
  <Grid.RowDefinitions>
       <RowDefinition Height="Auto"/>
       <RowDefinition Height="*"/>
  </Grid.RowDefinitions>
    <!--TitlePanel contains the name of the application and page title-->
  <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
      <TextBlock x:Name="ApplicationTitle" Text="F5DEBUG WP7 TUTORIALS" Style="{StaticResource PhoneTextNormalStyle}"/>
      <TextBlock x:Name="PageTitle" Text="Files N Folder" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
  </StackPanel>
    <!--ContentPanel - place additional content here-->
  <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <TextBlock Height="30" HorizontalAlignment="Left" Margin="15,41,0,0" Name="textBlock1" Text="File Name" VerticalAlignment="Top" />
    <TextBox Height="72" HorizontalAlignment="Left" Margin="132,19,0,0" Name="txtFileName" Text="" VerticalAlignment="Top" Width="304" />
    <TextBlock Height="30" HorizontalAlignment="Left" Margin="15,105,0,0" Name="textBlock2" Text="Folder Name" VerticalAlignment="Top" />
    <TextBox Height="72" HorizontalAlignment="Left" Margin="132,83,0,0" Name="txtFolderName" Text="" VerticalAlignment="Top" Width="304" />
    <TextBlock Height="30" HorizontalAlignment="Left" Margin="15,168,0,0" Name="textBlock3" Text="File Data" VerticalAlignment="Top" />
    <TextBox Height="149" HorizontalAlignment="Left" Margin="132,146,0,0" Name="txtFiledata" Text="" VerticalAlignment="Top" Width="304" />
    <Button Content="Save" Height="72" HorizontalAlignment="Left" Margin="9,357,0,0" Name="button1" VerticalAlignment="Top" Width="220" Click="button1_Click" />
    <CheckBox Content="Save in Root" Height="72" HorizontalAlignment="Left" Margin="-1,289,0,0" Name="cbroot" VerticalAlignment="Top" />
    <CheckBox Content="Save in Folder" Height="72" HorizontalAlignment="Right" Margin="0,289,45,0" Name="cbfolder" VerticalAlignment="Top" />
    <Button Content="Read" Height="72" HorizontalAlignment="Left" Margin="222,357,0,0" Name="button2" VerticalAlignment="Top" Width="211" Click="button2_Click" />
    <Button Content="Write to a Directory" Height="72" HorizontalAlignment="Left" Margin="12,418,0,0" Name="button3" VerticalAlignment="Top" Width="421"
Click="button3_Click" />
   <Button Content="Read from a Directory" Height="72" HorizontalAlignment="Left" Margin="12,478,0,0" Name="button4" VerticalAlignment="Top" Width="421" Click="button4_Click" />
   <Button Content="Delete a file Permanently" Height="72" HorizontalAlignment="Left" Margin="15,537,0,0" Name="button5" VerticalAlignment="Top" Width="421" Click="button5_Click" />
    </Grid>
</
Grid>

Now we are done with the design, let us start with the code behind to write the core logic to work with files and folders. First we need to import the namespace required for the Isolated Storage by adding the below 2 namespaces to the code behind.

Code Behind

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

Task 1 - Write text to a text file

Now our first task is to write some data to the text file and store it to the isolated storage. We need to use the IsolatedStorageFile class instance to get the user store for the specific application and to write the file we need to use the IsolatedStorageFileStream by providing the parameters that are required to store the details locally as shown in the code below.

Clipboard03.jpg

Code Behind

private void button1_Click(object sender, RoutedEventArgs e)
{
if (cbroot.IsChecked == true)
{
string strFilename = txtFileName.Text.ToString();
string strFileData = txtFiledata.Text.ToString();
IsolatedStorageFile ISFile = IsolatedStorageFile.GetUserStoreForApplication();
using (StreamWriter swfile = new StreamWriter(new IsolatedStorageFileStream(strFilename, FileMode.Create, FileAccess.Write, ISFile)))
{
swfile.WriteLine(strFileData);
swfile.Close();
}
MessageBox.Show("File Saved!!!");
}
else
{
MessageBox.Show("Select the checkbox to save file in root or in a folder");
}
}

Task 2 - Read the text file

Our next task is to read the text file from the root folder where we saved the text data in our previous task. For this task as well we need to use the IsolatedStorageFile Class and IsolatedStorageFileStream to get the data as shown in the code below.

Clipboard04.jpg

Code Behind

private void button2_Click(object sender, RoutedEventArgs e)
{
string strFilename = txtFileName.Text.ToString();
IsolatedStorageFile ISFile = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream ISFileStream = ISFile.OpenFile(strFilename, FileMode.Open, FileAccess.Read);
using (StreamReader reader = new StreamReader(ISFileStream))
{
string strData = reader.ReadLine();
MessageBox.Show(strData.ToString());
}
}


Task 3 - Delete the text file

In this task we are going to see how to delete the text file from the Isolated Storage which we created in our previous tasks. To delete the file we need to make use of the IsolatedStorageFile class and invoke the DeleteFile method by passing the filename directly as shown in the code below. Also to delete a folder we can use the DeleteDirectory method which do the similar process of deleting the directory completely.

Clipboard05.jpg

Code Behind

private void button5_Click(object sender, RoutedEventArgs e)
{
string strFilename = txtFileName.Text.ToString(); 
IsolatedStorageFile ISFile = IsolatedStorageFile.GetUserStoreForApplication();
ISFile.DeleteFile(strFilename);
}


Task 4 - Write some text to a text file inside a folder

Our next task is to write some text to a text file and save the file to a specific folder for easy data file maintenance with in the Isolated Storage. Normally this task is very important for developing the application with Windows Phone 7 to store the data locally in a folder to a specific folder. Copy the below code directly to the button click event to complete the task.

Clipboard06.jpg

Code Behind


private void button3_Click(object sender, RoutedEventArgs e)
{
if (cbfolder.IsChecked == true)
{
string strFileName = txtFileName.Text.ToString();
string strFileDirectory = txtFolderName.Text.ToString();
string strFileData = txtFiledata.Text.ToString();
IsolatedStorageFile ISFile = IsolatedStorageFile.GetUserStoreForApplication();
ISFile.CreateDirectory(strFileDirectory);
string strPath = strFileDirectory + "\\" + strFileName;
StreamWriter swWriter = new StreamWriter(new IsolatedStorageFileStream(strPath, FileMode.OpenOrCreate, ISFile));
swWriter.WriteLine(strFileData);
swWriter.Close();
MessageBox.Show("File Saved!!!");
}
else
{
MessageBox.Show("Select the checkbox to save file in root or in a folder");

}

Task 5 - Read the text file inside a folder


Our final task is to read the file which we create and saved in a particular folder in our previous task. This task uses the same IsolatedStorageFile and IsolatedStorageFileStream to read the data as shown in the code below.

Clipboard07.jpg

Code Behind

private void button4_Click(object sender, RoutedEventArgs e)
{
string strFilename = txtFileName.Text.ToString();
string strFileDirectory = txtFolderName.Text.ToString();
string strPath = strFileDirectory + "\\" + strFilename;
IsolatedStorageFile ISFile = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream ISFileStream = ISFile.OpenFile(strPath, FileMode.Open, FileAccess.Read);
using (StreamReader reader = new StreamReader(ISFileStream))
{
string strData = reader.ReadLine();
MessageBox.Show(strData.ToString());
}
}


Now we are done with the complete tasks and we can see the expected outputs as shown in the screen below. To build and execute the project simply press F5 from the keyboard or from the Visual Studio IDE tool bar.
Outputs Screen:

Clipboard08.jpg

Clipboard09.jpg

Conclusion:

So in this article we have seen the Files and Folders in Isolated Storage for Windows Phone 7 development using the IsolatedStorageFile class. In our next article we will see how to use the local storage database to save the data to read and write when ever required from the application.

 

 


Similar Articles