Tuesday, January 1, 2013

Windows Phone Working with Files and Folders


we can perform the below tasks on Isolated Storage for Files and Folders:

a)Write some text to a text file
b)Read the text file
c)Delete the text file
d)Write some text to a text file inside a folder
e)Read the text file inside a folder

for this purpose please make a project with name FilesAndFolders

1) copy the given layout to yours MainPage.xaml


<phone:PhoneApplicationPage
    x:Class="FilesAndFolders.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <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="Files And Folders " Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="Isolated Store" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

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


</phone:PhoneApplicationPage>

2) now copy the following given code to MainPage.xaml.cs


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 FilesAndFolders
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }
        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");
            }
        }


        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());
            }
        }
        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");
            }
        }
        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());
            }
        }


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


    }
}

3) run the application you can see the desired out put on emulator, to observe the all happenings IsoStorSpy tool. we can download it from the internet easily,





No comments:

Post a Comment