Tuesday, 12 March 2013

How to create a Textfile in Local Folder and perform Read Write Operations on it in Windows 8 Metro C#

hello guys,

today in this tutorial   I am going to talk about how to create a text file in the applications local folder and to perform read write operations on it.

So now open your Visual Studio and create a blank project.

Now first lets design our UI, to do that open your MainPage.Xaml.

we are going to require 3 buttons and 2 TextBoxes for our purpose,

1 button for file creation.

2 button for save

3 button for open

1 textbox for filename

1 textbox for filecontent

so our UI looks something like this

filetxt

the xaml code for the above design is as follows

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Button Content="Create File" HorizontalAlignment="Left" Margin="84,82,0,0" VerticalAlignment="Top" Width="147" Click="Button_Click_1"/>
<TextBox x:Name="filenamebox" HorizontalAlignment="Left" Margin="288,82,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="172"/>
<TextBox x:Name="txtboxtext" HorizontalAlignment="Left" Margin="615,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="376" Width="566"/>
<Button Content="Open" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="84,193,0,0" Width="147" Click="Button_Click_3"/>
<Button Content="Save" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="715,30,0,0" Width="147" Click="Button_Click_2"/>

</Grid>

now create event handlers for the buttons,

go to mainpage.xaml.cs

and add the following namespace

Using Windows.Storage;

now inside the create file buttons event handler write the following code

private async void Button_Click_1(object sender, RoutedEventArgs e)
{
await ApplicationData.Current.LocalFolder.CreateFileAsync(filenamebox.Text + ".txt");
}

this method contains asynchronous operation, so we need to add async keyword after the access specifier private.

in the above code we are creating a file in the applications local folder with the file name given in the textbox.

now inside the save file event handler , write the following code


private async void Button_Click_2(object sender, RoutedEventArgs e)
{
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(filenamebox.Text + ".txt");
await FileIO.WriteTextAsync(file, txtboxtext.Text);
}

in the above code we are writing the text from our textbox to the textfile inside the local folder.

now inside the open button event handler , add the following code


private async void Button_Click_3(object sender, RoutedEventArgs e)
{
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(filenamebox.Text + ".txt");
txtboxtext.Text = await FileIO.ReadTextAsync(file);
}


that is it, now we are done with the UI and Code, lets run our application and see weather it works or not

filetxt1 filetxt2 filetxt3

 

 

now even if you restart your application , you can still save the content,

Thanks for reading

hope you all liked the post

Subscribe to get updates

will see you soon with some other tutorial :)

5 comments :

  1. Fantastic items from you, man. I've take into accout your stuff previous to and you're just too excellent. I really like what you've acquired here, really like what you're stating and the way in which in which you assert it. You're making it enjoyable and you still take care of to keep it sensible. I cant wait to learn much more from you. That is really a terrific website.

    ReplyDelete
  2. thank you .. and stay tuned for the tutorials

    ReplyDelete
  3. Deepika sahu18 June 2013 11:40

    yap i like the post.. it's really good and easy to understand for me....... thank you

    ReplyDelete
  4. Why viewers still use to read news papers when in this technological world everything is accessible on web?

    ReplyDelete