How to save a text file in Windows Phone 7 or How to use isolated storage settings in Windows Phone 7?

By | April 3, 2012

Hello everyone…
Here is yet another simple tutorial for windows phone to save and edit a text file. This example uses isolatedStorage to do this. As like other platforms no this file created will be stored in the application sandbox and no other application can access it.
Here is the interface I created for saving a text file and reloading it.
Here is the xaml file for that.

    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>

        <!--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="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Button Content="Save" Height="72" HorizontalAlignment="Left" Margin="12,193,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
            <TextBox Height="72" HorizontalAlignment="Left" Margin="14,115,0,0" Name="textBox1" Text="" VerticalAlignment="Top" Width="454" />
            <Button Content="Load" Height="72" HorizontalAlignment="Left" Margin="178,193,0,0" Name="button2" VerticalAlignment="Top" Width="160" Click="button2_Click" />


Now the C# code for saving and loading data.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.IO.IsolatedStorage;

namespace IsolatedStorage
    public partial class MainPage : PhoneApplicationPage
        // Constructor
        public MainPage()

        /* Save the data in the textbox on the button click */
        private void button1_Click(object sender, RoutedEventArgs e)
            IsolatedStorageFile isolated_storage = IsolatedStorageFile.GetUserStoreForApplication();
            StreamWriter sw = new StreamWriter(new IsolatedStorageFileStream("myfolder\myfile.txt", FileMode.Create, isolated_storage));

        /* Load the data in the textbox on the button click */
        private void button2_Click(object sender, RoutedEventArgs e)
             IsolatedStorageFile isolated_storage = IsolatedStorageFile.GetUserStoreForApplication();
             StreamReader sr = null;
                 sr = new StreamReader(new IsolatedStorageFileStream("myfolder\myfile.txt", FileMode.Open, isolated_storage));
                 textBox1.Text = sr.ReadLine();
             catch (Exception e1)
                 textBox1.Text = "Error : " + e1.Message;

Please leave your valuable comments on this post.

Website Pin Facebook Twitter Myspace Friendfeed Technorati Digg Google StumbleUpon Premium Responsive

Leave a Reply

Your email address will not be published. Required fields are marked *