Saturday, June 2, 2012

How to use isolated storage API in Windows 8 Application


While working on Windows 8 Application, I was under impression, is that we can use the Isolated storage API as it is like Windows Phone 7, but we can’t in Windows 8, Windows 8 introduce new API that is Windows.Storage. Storage API allows us to save data in Application level data. This is very simple and useful code snippet here.

  1: public void SaveInfo(string key, string value)
  2: {
  3:     if (Windows.Storage.ApplicationData.Current.LocalSettings.Values.ContainsKey(key))
  4:     {
  5:         if (Windows.Storage.ApplicationData.Current.LocalSettings.Values[key].ToString() != null)
  6:         {
  7:             // do update
  8:             Windows.Storage.ApplicationData.Current.LocalSettings.Values[key] = value;
  9:         }
 10:     }
 11:     else
 12:     {
 13:         // do create key and save value, first time only.
 14: 
 15:         Windows.Storage.ApplicationData.Current.LocalSettings.CreateContainer(key, ApplicationDataCreateDisposition.Always);
 16: 
 17:         if (Windows.Storage.ApplicationData.Current.LocalSettings.Values[key] == null)
 18:         {
 19:             Windows.Storage.ApplicationData.Current.LocalSettings.Values[key] = value;
 20:         }
 21:     }
 22: }



Cheer!!

Happy coding… :)

6 comments:

Rakesh said...

can you please help me in create a word doc in local storage and launching the same in windows phone 8.
Any help would be appreciated.

Thanks,
Rakesh.

Unknown said...

BTW, none of these APIs are implemented on Windows Phone 8. They'll just throw NotImplementedExceptions.

Pavan Pareta said...

This API is only available for Windows 8 Application development. NOT for Windows Phone 8 Application development.

... said...

That is a pity!
It would be great to have a single storage solution for both Windows 8 and Windows Phone 8.

I'm using the RoamingSettings in a Windows 8 app and hoped I could use the same in a Windows Phone 8 app to access the same data as the Windows 8 app.

pushpen said...

this is a good code to save data. but if i want to retrieve data than how can i do that ? please provide code for that too.

Sarah Taylor said...

It is a good practice to share your experience with others to give a helping hand to others.windows 8 application development