Local Data in Windows 8

This article shows how to use Windows 8 local data storage.

Introduction

Playing with data is not easy in any dynamic app. You need to first decide where to put it. Second, what is the role of the data?

And, in every aspect you need to use different lines of code.

Whether it is XML, database, Text File, Application Setting or simple local data.

So this article will explain the local data storage.

Structure of Demo

In a XAML page, we will first save some value (let it be my Name) and then in the next XAML page we will try to fetch that stored data.

Procedure

Step1: Start a new Blank App project from Windows Store. And add an extra XAML page in Solution Explorer.
Step 2: Before trying anything new add Windows.Storage and Windows.UI.Popups. Since we will use their class in this app.

Try to design something like this:

design

From here, we will try to code the Save button.
 

// Text File Name

const string fileName="TestWindows8App.txt";

StorageFolder myFolder=null;

StorageFile myFile=null;

 

private async void Button_Click_1(object sender, RoutedEventArgs e)

{

    //Save Button

    myFolder = ApplicationData.Current.RoamingFolder;

    myFile=await myFolder.CreateFileAsync(fileName,CreationCollisionOption.ReplaceExisting);

    await FileIO.WriteTextAsync(myFile,"Hello Mr. "+myTextBox.Text.ToString());

     //Popup Message

     var messageBox = new MessageDialog("Successfully Saved to "+fileName+"  :) ");

     await messageBox.ShowAsync();

}

Many of those lines are easily understood if you have keen eyes. Though, I will explain.

In the first line, we try to get the Roaming Folder and put it inside myFolder (in other words an instance of StorageFolder).

Similarly, we will create a file with a specified name (in other words fileName).

After doing all this, we try to write the data to the Text file. Finally, we pop up a message box to show a successful report.

Then, we click on a Button that navigates to to the next page as in the following:

private void Button_Click(object sender, RoutedEventArgs e)

{

    //navigate to New Page

    this.Frame.Navigate(typeof(NewPage));

}

Here, NewPage is the name of my XAML page (in other words NewPage.xaml).

NewPage

This page is now completed and now we will move to Newpage.xaml.

And NewPage.xaml looks like:

And in the Read button we code something like this:

const string fileName = "TestWindows8App.txt";

StorageFile stoFile;

StorageFolder stofolder=null;

 

private async void Button_Click(object sender, RoutedEventArgs e)

{

     //Here, I will Read from Roaming Folder

    stofolder = ApplicationData.Current.RoamingFolder; // Get The Roaming Directory

    stoFile =await stofolder.GetFileAsync(fileName); // Try to get the exact file

    string tempRead =await FileIO.ReadTextAsync(stoFile); // Read from thab  file           

    myReadTextBox.Text = tempRead.ToString(); //Put in TextBox

}

Most of the lines are similar to the previous one, except the ReadTexAsync() method.

After a successful read operation, it will be assigned to the myReadTextBox.

The outcome will be:

Outcome

When we click on the Navigate button:
 
Navigate button

Conclusion

This is not something exceptional but you can use it as you need to.

Regarding this article, if you encounter any problem then try to get it from the solution file. Or, ping me.

Stay Raw, Stay Geek and Keep Coding.