Introduction

In this article we will learn how to access files in a Windows 8 Metro Style Application. Here, we will explain using an example that shows how to access a file. In this example we will present various points with various functionality such as how to create a new file in text form and writing to a file, reading a file, copying a file, deleting a file.

So, we will use the following steps to make this application as below.

Step 1 : First of all you will create a new Metro Style Application. Let us see the description with images of how you will create it.

openpage.gif

homepage.gif

Step 2 : In the Solution Explorer there are two files that we will primarily work with; MainPage.xaml and MainPage.xaml.cs files. In the images folder add any image to the application but in this application we don't have to add an image.

solutionexplorer.gif

Step 3 : The MainPage.xaml file is as in the following code.

Code : Let us see the code which is given below:

<UserControl x:Class="FileAccessSample.MainPage"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

mc:Ignorable="d"

d:DesignHeight="968" d:DesignWidth="1466">

<!--App Orientation States-->

<VisualStateManager.VisualStateGroups>

<VisualStateGroup x:Name="OrientationStates">

<VisualState x:Name="Full"/>

<VisualState x:Name="Fill">

<Storyboard>

<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="ContentRoot">

<DiscreteObjectKeyFrame KeyTime="0">

<DiscreteObjectKeyFrame.Value>

<Thickness>40,20,40,20</Thickness>

</DiscreteObjectKeyFrame.Value>

</DiscreteObjectKeyFrame>

</ObjectAnimationUsingKeyFrames>

<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(StackPanel.Orientation)" Storyboard.TargetName="InputPanel">

<DiscreteObjectKeyFrame KeyTime="0">

<DiscreteObjectKeyFrame.Value>

<Orientation>Horizontal</Orientation>

</DiscreteObjectKeyFrame.Value>

</DiscreteObjectKeyFrame>

</ObjectAnimationUsingKeyFrames>

<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.MaxWidth)" Storyboard.TargetName="Description">

<DiscreteObjectKeyFrame KeyTime="0" Value="700">

</DiscreteObjectKeyFrame>

</ObjectAnimationUsingKeyFrames>

</Storyboard>

</VisualState>

<VisualState x:Name="Portrait">

<Storyboard>

<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="ContentRoot">

<DiscreteObjectKeyFrame KeyTime="0">

<DiscreteObjectKeyFrame.Value>

<Thickness>40,20,40,20</Thickness>

</DiscreteObjectKeyFrame.Value>

</DiscreteObjectKeyFrame>

</ObjectAnimationUsingKeyFrames>

<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.MaxWidth)" Storyboard.TargetName="Description">

<DiscreteObjectKeyFrame KeyTime="0" Value="700">

</DiscreteObjectKeyFrame>

</ObjectAnimationUsingKeyFrames>

</Storyboard>

</VisualState>

<VisualState x:Name="Snapped">

<Storyboard>

<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="ContentRoot">

<DiscreteObjectKeyFrame KeyTime="0">

<DiscreteObjectKeyFrame.Value>

<Thickness>20,20,20,20</Thickness>

</DiscreteObjectKeyFrame.Value>

</DiscreteObjectKeyFrame>

</ObjectAnimationUsingKeyFrames>

<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(StackPanel.Orientation)" Storyboard.TargetName="InputPanel">

<DiscreteObjectKeyFrame KeyTime="0">

<DiscreteObjectKeyFrame.Value>

<Orientation>Vertical</Orientation>

</DiscreteObjectKeyFrame.Value>

</DiscreteObjectKeyFrame>

</ObjectAnimationUsingKeyFrames>

<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.MaxWidth)" Storyboard.TargetName="Description">

<DiscreteObjectKeyFrame KeyTime="0" Value="250">

</DiscreteObjectKeyFrame>

</ObjectAnimationUsingKeyFrames>

<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="LegalPanel">

<DiscreteObjectKeyFrame KeyTime="0">

<DiscreteObjectKeyFrame.Value>

<Thickness>0,0,10,0</Thickness>

</DiscreteObjectKeyFrame.Value>

</DiscreteObjectKeyFrame>

</ObjectAnimationUsingKeyFrames>

</Storyboard>

</VisualState>

</VisualStateGroup>

</VisualStateManager.VisualStateGroups>

<Grid x:Name="ContentRoot" Background="PaleGreen" Margin="100,20,100,20">

<Grid.RowDefinitions>

<RowDefinition Height="Auto"/>

<RowDefinition Height="*"/>

<RowDefinition Height="Auto"/>

</Grid.RowDefinitions>

<!-- Content -->

<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Grid.Row="1" ZoomMode="Disabled">

<StackPanel x:Name="ContentPanel">

<StackPanel x:Name="InputPanel" Orientation="Horizontal" HorizontalAlignment="Left">

<StackPanel>

<TextBlock Text="Input" Style="{StaticResource H2Style}"/>

<TextBlock Text="Select Section:" Style="{StaticResource H3Style}"/>

<ListBox x:Name="SectionList" Margin="0,0,20,0" HorizontalAlignment="Left">

<ListBox.ItemTemplate >

<DataTemplate >

<TextBlock Text="{Binding Name}"/>

</DataTemplate>

</ListBox.ItemTemplate>

<ListBoxItem x:Name="Section1" Background="Bisque">

<TextBlock Style="{StaticResource ListBoxTextStyle}" Text="1. Creating a file" />

</ListBoxItem>

<ListBoxItem x:Name="Section2" Background="Aquamarine">

<TextBlock Style="{StaticResource ListBoxTextStyle}" Text="2. Writing to a file" />

</ListBoxItem>

<ListBoxItem x:Name="Section3" Background="BurlyWood">

<TextBlock Style="{StaticResource ListBoxTextStyle}" Text="3. Reading a file" />

</ListBoxItem>

<ListBoxItem x:Name="Section4" Background="LightSlateGray">

<TextBlock Style="{StaticResource ListBoxTextStyle}" Text="4. Copying a file" />

</ListBoxItem>

<ListBoxItem x:Name="Section5" Background="PaleTurquoise">

<TextBlock Style="{StaticResource ListBoxTextStyle}" Text="5. Deleting a file" />

</ListBoxItem>

</ListBox>

</StackPanel>

<StackPanel Margin="0,31,0,0" >

<TextBlock Text="Description:" Style="{StaticResource H3Style}"/>

<StackPanel x:Name="Description" MaxWidth="900">

<!-- Section 1 -->

<StackPanel x:Name="Section1Input">

<TextBlock Style="{StaticResource DescriptionTextStyle}" TextWrapping="Wrap" Text="Create a new file sample.txt to exercise the Sections in this sample." HorizontalAlignment="Left"/>

<StackPanel Orientation="Horizontal" Margin="0,50,0,0">

<Button x:Name="CreateFileButton" Content="Create File" Margin="0,10,10,0"/>

</StackPanel>

</StackPanel>

<!-- Section 2 -->

<StackPanel x:Name="Section2Input">

<TextBlock Style="{StaticResource DescriptionTextStyle}" TextWrapping="Wrap" Text="To write something to sample.txt type something in the textbox below and click 'Write.'" HorizontalAlignment="Left"/>

<StackPanel Orientation="Vertical">

<TextBox x:Name="Section2Textbox" IsReadOnly="false" HorizontalAlignment="Left" VerticalAlignment="Top" Width="500" Height="150" TextWrapping="Wrap" AcceptsReturn="True" Text=""/>

<StackPanel Orientation="Horizontal">

<Button x:Name="WriteButton" Content="Write"/>

</StackPanel>

</StackPanel>

</StackPanel>

<!-- Section 3 -->

<StackPanel x:Name="Section3Input">

<TextBlock Style="{StaticResource DescriptionTextStyle}" TextWrapping="Wrap" Text="To read from sample.txt click 'Read.'" HorizontalAlignment="Left"/>

<StackPanel Orientation="Horizontal" Margin="0,50,0,0">

<Button x:Name="ReadButton" Content="Read" Margin="0,10,10,0"/>

</StackPanel>

</StackPanel>

<!—Section 4 -->

<StackPanel x:Name="Section6Input">

<TextBlock Style="{StaticResource DescriptionTextStyle}" TextWrapping="Wrap" Text="To copy sample.txt click 'Copy'" HorizontalAlignment="Left"/>

<StackPanel Orientation="Horizontal" Margin="0,50,0,0">

<Button x:Name="CopyButton" Content="Copy" Margin="0,10,10,0"/>

</StackPanel>

</StackPanel>

<!-- Section 5 -->

<StackPanel x:Name="Section7Input">

<TextBlock Style="{StaticResource DescriptionTextStyle}" TextWrapping="Wrap" Text="To delete sample.txt click 'Delete'" HorizontalAlignment="Left"/>

<StackPanel Orientation="Horizontal" Margin="0,50,0,0">

<Button x:Name="DeleteButton" Content="Delete" Margin="0,10,10,0"/>

</StackPanel>

</StackPanel>

</StackPanel>

</StackPanel>

</StackPanel>

<!-- Output section -->

<TextBlock Text="Output" Margin="0,25,0,20" Style="{StaticResource H2Style}"/>

<StackPanel x:Name="Output" HorizontalAlignment="Left">

<!-- Section 1 -->

<StackPanel x:Name="Section1Output">

<TextBlock x:Name="Section1Output_textblock" Style="{StaticResource DescriptionTextStyle}" TextWrapping="Wrap" Text="Create a new file above." />

</StackPanel>

<!-- Section 2 -->

<StackPanel x:Name="Section2Output">
<
TextBlock x:Name ="Section2Output_textblock" Style="{StaticResource DescriptionTextStyle}" TextWrapping="Wrap" Text="Enter text above to be written to sample.txt" />

</StackPanel>

<!-- Section 3 -->

<StackPanel x:Name="Section3Output">

<TextBlock x:Name ="Section3Output_textblock" Style="{StaticResource DescriptionTextStyle}" TextWrapping="Wrap" Text="" />

</StackPanel>

<!-- Section 4 -->

<StackPanel x:Name="Section4Output">

<TextBlock x:Name ="Section4Output_textblock" Style="{StaticResource DescriptionTextStyle}" TextWrapping="Wrap" Text="" />

</StackPanel>

<!-- Section 5 -->

<StackPanel x:Name="Section5Output">

<TextBlock x:Name ="Section5Output_textblock" Style="{StaticResource DescriptionTextStyle}" TextWrapping="Wrap" Text="" />

</StackPanel>

</StackPanel>

</StackPanel>

</ScrollViewer>

</StackPanel>

</Grid>

</UserControl>

Step 4 : After running this code we get the following output. Here we have to explain the five different points with various functionality.

output.gif

Creating a file.

output1.1.gif

Writing a file.

output1.2.gif

Reading a file.

output1.3.gif

Copying a file.

output1.4.gif

Delete a file.

output1.5.gif