Local Data in Windows 8

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

 
Step 1
 
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.
  1. // Text File Name  
  2. const string fileName="TestWindows8App.txt";  
  3. StorageFolder myFolder=null;  
  4. StorageFile myFile=null;  
  5.    
  6. private async void Button_Click_1(object sender, RoutedEventArgs e)  
  7. {  
  8.     //Save Button  
  9.     myFolder = ApplicationData.Current.RoamingFolder;  
  10.     myFile=await myFolder.CreateFileAsync(fileName,CreationCollisionOption.ReplaceExisting);  
  11.     await FileIO.WriteTextAsync(myFile,"Hello Mr. "+myTextBox.Text.ToString());  
  12.      //Popup Message  
  13.      var messageBox = new MessageDialog("Successfully Saved to "+fileName+"  :) ");  
  14.      await messageBox.ShowAsync();  
  15. }   
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 the next page as in the following.
  1. private void Button_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     //navigate to New Page  
  4.     this.Frame.Navigate(typeof(NewPage));  
  5. }   
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.
  1. const string fileName = "TestWindows8App.txt";  
  2. StorageFile stoFile;  
  3. StorageFolder stofolder=null;  
  4.    
  5. private async void Button_Click(object sender, RoutedEventArgs e)  
  6. {  
  7.      //Here, I will Read from Roaming Folder  
  8.     stofolder = ApplicationData.Current.RoamingFolder; // Get The Roaming Directory  
  9.     stoFile =await stofolder.GetFileAsync(fileName); // Try to get the exact file  
  10.     string tempRead =await FileIO.ReadTextAsync(stoFile); // Read from thab  file             
  11.     myReadTextBox.Text = tempRead.ToString(); //Put in TextBox  
  12. }   
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. 


Similar Articles