Floating FB popout byF5debug

Learn Windows Phone 7 Development in 31 Days – Day 25 – 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.      [more]

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.

clip_image002

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.

clip_image004

XAML Code:

01

<Grid x:Name="LayoutRoot" Background="Transparent">

02

<Grid.RowDefinitions>

03

<RowDefinition Height="Auto"/>

04

<RowDefinition Height="*"/>

05

</Grid.RowDefinitions>

06

 

07

<!--TitlePanel contains the name of the application and page title-->

08

<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

09

<TextBlock x:Name="ApplicationTitle" Text="F5DEBUG WP7 TUTORIALS" Style="{StaticResource PhoneTextNormalStyle}"/>

10

<TextBlock x:Name="PageTitle" Text="Files N Folder" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

11

</StackPanel>

12

 

13

<!--ContentPanel - place additional content here-->

14

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

15

<TextBlock Height="30" HorizontalAlignment="Left" Margin="15,41,0,0" Name="textBlock1" Text="File Name" VerticalAlignment="Top" />

16

<TextBox Height="72" HorizontalAlignment="Left" Margin="132,19,0,0" Name="txtFileName" Text="" VerticalAlignment="Top" Width="304" />

17

<TextBlock Height="30" HorizontalAlignment="Left" Margin="15,105,0,0" Name="textBlock2" Text="Folder Name" VerticalAlignment="Top" />

18

<TextBox Height="72" HorizontalAlignment="Left" Margin="132,83,0,0" Name="txtFolderName" Text="" VerticalAlignment="Top" Width="304" />

19

<TextBlock Height="30" HorizontalAlignment="Left" Margin="15,168,0,0" Name="textBlock3" Text="File Data" VerticalAlignment="Top" />

20

<TextBox Height="149" HorizontalAlignment="Left" Margin="132,146,0,0" Name="txtFiledata" Text="" VerticalAlignment="Top" Width="304" />

21

<Button Content="Save" Height="72" HorizontalAlignment="Left" Margin="9,357,0,0" Name="button1" VerticalAlignment="Top" Width="220" Click="button1_Click" />

22

<CheckBox Content="Save in Root" Height="72" HorizontalAlignment="Left" Margin="-1,289,0,0" Name="cbroot" VerticalAlignment="Top" />

23

<CheckBox Content="Save in Folder" Height="72" HorizontalAlignment="Right" Margin="0,289,45,0" Name="cbfolder" VerticalAlignment="Top" />

24

<Button Content="Read" Height="72" HorizontalAlignment="Left" Margin="222,357,0,0" Name="button2" VerticalAlignment="Top" Width="211" Click="button2_Click" />

25

<Button Content="Write to a Directory" Height="72" HorizontalAlignment="Left" Margin="12,418,0,0" Name="button3" VerticalAlignment="Top" Width="421" Click="button3_Click" />

26

<Button Content="Read from a Directory" Height="72" HorizontalAlignment="Left" Margin="12,478,0,0" Name="button4" VerticalAlignment="Top" Width="421" Click="button4_Click" />

27

<Button Content="Delete a file Permanently" Height="72" HorizontalAlignment="Left" Margin="15,537,0,0" Name="button5" VerticalAlignment="Top" Width="421" Click="button5_Click" />

28

</Grid>

29

</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:

1

using System.IO;

2

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.

clip_image006

Code Behind:

01

private void button1_Click(object sender, RoutedEventArgs e)

02

{

03

if (cbroot.IsChecked == true)

04

{

05

string strFilename = txtFileName.Text.ToString();

06

string strFileData = txtFiledata.Text.ToString();

07

IsolatedStorageFile ISFile = IsolatedStorageFile.GetUserStoreForApplication();

08

using (StreamWriter swfile = new StreamWriter(new IsolatedStorageFileStream(strFilename, FileMode.Create, FileAccess.Write, ISFile)))

09

{

10

swfile.WriteLine(strFileData);

11

swfile.Close();

12

}

13

 

14

MessageBox.Show("File Saved!!!");

15

}

16

else

17

{

18

MessageBox.Show("Select the checkbox to save file in root or in a folder");

19

}

20

 

21

}

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.

clip_image008

Code Behind:

01

private void button2_Click(object sender, RoutedEventArgs e)

02

{

03

string strFilename = txtFileName.Text.ToString();

04

 

05

IsolatedStorageFile ISFile = IsolatedStorageFile.GetUserStoreForApplication();

06

IsolatedStorageFileStream ISFileStream = ISFile.OpenFile(strFilename, FileMode.Open, FileAccess.Read);

07

using (StreamReader reader = new StreamReader(ISFileStream))

08

{

09

string strData = reader.ReadLine();

10

MessageBox.Show(strData.ToString());

11

}

12

}

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.

clip_image010

Code Behind:

1

private void button5_Click(object sender, RoutedEventArgs e)

2

{

3

string strFilename = txtFileName.Text.ToString();

4

 

5

IsolatedStorageFile ISFile = IsolatedStorageFile.GetUserStoreForApplication();

6

ISFile.DeleteFile(strFilename);

7

}

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.

clip_image012

Code Behind:

01

private void button3_Click(object sender, RoutedEventArgs e)

02

{

03

if (cbfolder.IsChecked == true)

04

{

05

string strFileName = txtFileName.Text.ToString();

06

string strFileDirectory = txtFolderName.Text.ToString();

07

string strFileData = txtFiledata.Text.ToString();

08

 

09

IsolatedStorageFile ISFile = IsolatedStorageFile.GetUserStoreForApplication();

10

ISFile.CreateDirectory(strFileDirectory);

11

 

12

string strPath = strFileDirectory + "\\" + strFileName;

13

 

14

StreamWriter swWriter = new StreamWriter(new IsolatedStorageFileStream(strPath, FileMode.OpenOrCreate, ISFile));

15

swWriter.WriteLine(strFileData);

16

swWriter.Close();

17

MessageBox.Show("File Saved!!!");

18

}

19

else

20

{

21

MessageBox.Show("Select the checkbox to save file in root or in a folder");

22

}

23

 

24

}

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.

clip_image014

Code Behind:

01

private void button4_Click(object sender, RoutedEventArgs e)

02

{

03

string strFilename = txtFileName.Text.ToString();

04

string strFileDirectory = txtFolderName.Text.ToString();

05

string strPath = strFileDirectory + "\\" + strFilename;

06

 

07

IsolatedStorageFile ISFile = IsolatedStorageFile.GetUserStoreForApplication();

08

IsolatedStorageFileStream ISFileStream = ISFile.OpenFile(strPath, FileMode.Open, FileAccess.Read);

09

using (StreamReader reader = new StreamReader(ISFileStream))

10

{

11

string strData = reader.ReadLine();

12

MessageBox.Show(strData.ToString());

13

}

14

}

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:

clip_image015

clip_image017

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.

About Author: Karthikeyan Anbarasan, Microsoft MVP (Most Valuable Professional) in ASP.NET/IIS Architecture. He is the Founder and Chief Editor of the www.f5debug.net website and has authored books on Windows Phone and Business Intelligence(SSIS). He is also a Passionate Speaker and a Blogger on Microsoft Technologies.

You can Join Me On: Facebook, Twitter, Google Plus, LinkedIn

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Recent Posts

Random Posts

Most Viewed

URL Shortner

Create your own short urls!!!

Site Counter

free counters

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet. I have documented my personal experience on this blog.

Protected by Copyscape Web Plagiarism Finder

Site Meter