Windows Phone 8 Storage

In this part, we will learn about Windows Phone files and folders storage. Basically applications require a location to store data locally to be manipulated. In Windows Phone 8, it is called Isolated Storage that is accessible only to a specific application. It can be used in three ways as shown below.

  • Key/Value Pair
  • Files and Folders
  • Local Database

Now we will see the key/value pair method.

Steps:

  1. Create a new Silverlight Windows Phone 8 project with a valid project name. Here I'm using the project name ExampleKVP.
  2. Now add a control to get the user input to store it to and retrieve it from isolated storage. I will add one TextBox to get the input from the user, one Textblock to retrieve (show) the data and three Buttons, one is for saving, one is to retrieve and another one is to delete the data. You can see the design screen below.


XAML code:

  1. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">  
  2.             <TextBlock HorizontalAlignment="Left" Margin="68,154,0,0" TextWrapping="Wrap" Text="User Input :" VerticalAlignment="Top"/>  
  3.             <TextBlock x:Name="retrivetxtblock" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Margin="68,379,0,0" Height="162" Width="358"/>  
  4.             <TextBox HorizontalAlignment="Left" Height="72" Margin="166,131,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="260"/>  
  5.             <Button x:Name="viewbtn" Content="View" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="161,276,0,0" Width="123"/>  
  6.             <Button x:Name="deletebtn" Content="Delete" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="289,276,0,0" Width="120"/>  
  7.             <Button x:Name="savebtn" Content="Save" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="34,276,0,0" Width="122"/>  
  8.  </Grid> 

Now we will do the coding part. Open your “ MainPage.xaml.cs” page and we need to add the following namespace:

  1. using System.IO.IsolatedStorage; 

Now write the following code to save the data.

  1. private void savebtn_Click(object sender, RoutedEventArgs e)  
  2. {  
  3. IsolatedStorageSettings setting = IsolatedStorageSettings.ApplicationSettings;  
  4. if (!setting.Contains("KeyName"))  
  5. setting.Add("KeyName", userinputtxtbox.Text);  
  6. else  
  7. setting["KeyName"] = userinputtxtbox.Text;  
  8. setting.Save();  

In this code “KeyName” is your data. The key name is used to save the data typed into the userinputtxtbox by the user.

First we will create an instance of IsolatedStoragrSetting and using that instance add the user input using the “Add” method and finally save that data using the “Save” method.

Next we will retrieve the data that is already saved. Write the following code in the viewbtn click event.

  1. private void viewbtn_Click(object sender, RoutedEventArgs e)  
  2. {  
  3. if (IsolatedStorageSettings.ApplicationSettings.Contains("KeyName"))  
  4. {  
  5. retrivetxtblock.Text = IsolatedStorageSettings.ApplicationSettings["KeyName"as string;  
  6. }  
  7. else  
  8. {  
  9. MessageBox.Show("No Key Found");  
  10. }  

It is very simple to retrieve the data by using the “Contains” method and display it to the “retrivetxtblock”.

Finally, we will write the code to delete the data from isolated storage. Write the following code to delete the data.

  1. private void deletebtn_Click(object sender, RoutedEventArgs e)  
  2. {  
  3. if(IsolatedStorageSettings.ApplicationSettings.Contains("KeyName")  
  4. IsolatedStorageSettings.ApplicationSettings.Remove("KeyName");  
  5. else  
  6.   
  7. MessageBox.Show("No Key Found");  

It is also very simple to delete the data. We will check whether the key is available. If the key is available then delete it using the “Remove” method.

The overall code page is as shown below.




C# Code

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Navigation;  
  8. using Microsoft.Phone.Controls;  
  9. using Microsoft.Phone.Shell;  
  10. using ExampleKVP.Resources;  
  11. using System.IO.IsolatedStorage;  
  12.   
  13. namespace ExampleKVP  
  14. {  
  15. public partial class MainPage : PhoneApplicationPage  
  16. {  
  17. // Constructor  
  18. public MainPage()  
  19. {  
  20. InitializeComponent();  
  21. }  
  22. private void savebtn_Click(object sender, RoutedEventArgs e)  
  23. {  
  24. IsolatedStorageSettings setting = IsolatedStorageSettings.ApplicationSettings;  
  25. if (!setting.Contains("KeyName"))  
  26. setting.Add("KeyName", userinputtxtbox.Text);  
  27. else  
  28. {  
  29. setting["KeyName"] = userinputtxtbox.Text;  
  30. MessageBox.Show("data saved sucessfully");  
  31. }  
  32. setting.Save();  
  33.   
  34. }  
  35. private void viewbtn_Click(object sender, RoutedEventArgs e)  
  36. {  
  37. if (IsolatedStorageSettings.ApplicationSettings.Contains("KeyName"))  
  38. {  
  39. retrivetxtblock.Text = IsolatedStorageSettings.ApplicationSettings["KeyName"as string;  
  40. }  
  41. else  
  42. {  
  43. MessageBox.Show("No Key Found");  
  44. }  
  45. }  
  46. private void deletebtn_Click(object sender, RoutedEventArgs e)  
  47. {  
  48. if(IsolatedStorageSettings.ApplicationSettings.Contains("KeyName"))  
  49. {  
  50. IsolatedStorageSettings.ApplicationSettings.Remove("KeyName");  
  51. MessageBox.Show("data deleted");  
  52. }  
  53. else  
  54. MessageBox.Show("No Key Found");  
  55. }  
  56. }  

Let's run the application by pressing F5 and see the output as shown below.



Up Next
    Ebook Download
    View all
    Learn
    View all