Floating FB popout byF5debug

How to store a Key Value Pair in Windows Phone

 

In this article we are going to see what are the possible ways we can have a Key Value Pair data stored in Windows Phone and how you can effectively utilize in storing some information or application settings which can provide a better approach to handle with in the application. In Windows Phone data can be manipulated and stored in multiple ways and also can be retrieved and used in multiple ways based on the requirement and performance. In real time we need to have a couple of flags or local information that should pertain to the application level which we cant really depend on a Cloud enabled storage or to have in some other medium.

To overcome this with Isolated storage we can store the application specific information in a key value pair and can be accessed with in the application effectively. Isolated Storage name suggests, the file or the folder stored with the Isolated Storage content will be available for the specific application only and isolated from the other application trying to access. Therefore, the main advantage over the isolated storage system is each application will be independent on to its storage medium.

Isolated Storage are available in System.IO.IsolatedStorage name space on which we have IsolatedStorageSettings class which has ApplicationSettings property to store and retrieve the key value pair in a effective and efficient manner.

Code to Store the Value:

The below code checks first the ApplicationSettings holds any key with the name AppNameDB, if yes just the code prompts a message saying the Key is available (You need to put your business logic here what need to be done when the key is available). If the key is not available then it goes to the else condition where we have assigned a value to the key and from then the Key has that value available till we manually delete the key from the isolated storage.

private void InsertKeyValue_Click(object sender, RoutedEventArgs e)
        {
            IsolatedStorageSettings ISsetting = IsolatedStorageSettings.ApplicationSettings;
            if (!ISsetting.Contains("AppNameDB"))
            {
                ISsetting.Add("AppNameDB", "Karthik");
                MessageBox.Show("Value saved sucessfully");
            }
            else
            {
                ISsetting["AppNameDB"] ="Karthik";
                MessageBox.Show("Value saved sucessfully");
            }
            ISsetting.Save();
        }

 

Code to Retrieve the Value:

In the below code to retrieve the value for a key just we need to access the ApplicationSettings propery and check if it contains the key which we are looking out, else we can have a loop and navigate through the list of available keys and get the one which need to be manipulated.

private void RetriveKey_Click(object sender, RoutedEventArgs e)
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains("AppNameDB"))
            {
                var myKeyName = IsolatedStorageSettings.ApplicationSettings["AppNameDB"] as string;
            }
            else
            {
                MessageBox.Show("Key Not Found!!!");
            }

        }

 

Code to Delete the Key Permanently:

In the below code we check if the Key is available in the Isolated Storage, if its available we can use the Remove method and delete the key from the storage by passing the key name correctly. If we dont have the correct key name then the respective key will not be deleted from the storage.

private void DeleteKey_Click(object sender, RoutedEventArgs e)
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains("AppNameDB"))
            {
                IsolatedStorageSettings.ApplicationSettings.Remove("AppNameDB");
                MessageBox.Show("Key Value deleted");
            }
            else
                MessageBox.Show("Key Not Found!!!");
        }

About Author: Karthikeyan Anbarasan, Microsoft MVP (Most Valuable Professional) in ASP.NET/IIS Architecture. He is the Founder and Chief Editor of the www.f5debug.net website and has authored books on Windows Phone and Business Intelligence(SSIS). He is also a Passionate Speaker and a Blogger on Microsoft Technologies.

You can Join Me On: Facebook, Twitter, Google Plus, LinkedIn

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Recent Posts

Random Posts

Most Viewed

URL Shortner

Create your own short urls!!!

Site Counter

free counters

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet. I have documented my personal experience on this blog.

Protected by Copyscape Web Plagiarism Finder

Site Meter