Question

my goal is retrieve value of counter which was stored last time when application closed. i.e. i am storing the counter value in isolated storage. if counter has value 5 and application is closed and again started i should be able to retrieve 5. for this purpose i have written following code but i cannot make out of it.

IsolatedStorageSettings isoStoreSettings = IsolatedStorageSettings.ApplicationSettings;
int count=0;
isoStoreSettings["flag"];
if (isoStoreSettings["flag"]!="set")
{                
   isoStoreSettings["count"] = count;
   isoStoreSettings["flag"] = "set";
}


count = isoStorageSettings["count"];  //using the value of count stored previously

//some code which updates the count variable

isoStorageSettings["count"]=count;

the problem with this code is that declaration of key in isolatedstorage is now allowed, we must assign some value to that key but if i assign value to that key, it will reinitialize the key each time application is started. so, if anyone can solve this, please help even there is any other alternative to isolatedstorage for my goal then also please share.

Was it helpful?

Solution

If you want to load your count every time the App is Launched then put your code in Application_Launching event in App.xaml.cs:

// declare static variable which you will be able to access from anywhere
public static int count;

private void Application_Launching(object sender, LaunchingEventArgs e)
{
    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
    if (settings.Contains("count")) count = (int)settings["count"];
    else count = 0;
}

On Clising event - save your variable:

private void Application_Closing(object sender, ClosingEventArgs e)
{
    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
    if (settings.Contains("count")) settings["count"] = count;
    else settings.Add("count", count);
    settings.Save();
}

From anywhere in your code, you should be able to access your variable like this:

int myVariable = App.count;
App.count++; 
// and so on

Note that you may also think about Activated and Deactivated events - for more information read MSDN.

I also don't know what flag suppose to do, but code above should save your variable just fine.

OTHER TIPS

Use updated count value in Add method like below:

    IsolatedStorageSettings isolatedStorageSettingsCount = IsolatedStorageSettings.ApplicationSettings;
if (isolatedStorageSettingsCount.Contains("count"))
            {
                isolatedStorageSettingsCount.Remove("count");
                isolatedStorageSettingsCount.Add("count",5);
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
            else
            {
                isolatedStorageSettingsCount.Add("count",5);
                IsolatedStorageSettings.ApplicationSettings.Save();
            }

To retrive your count value use below code:

IsolatedStorageSettings isolatedStorageSettingsCount = IsolatedStorageSettings.ApplicationSettings;
string strCount = (string)isolatedStorageSettingsCount["count"];
int count=Convert.ToInt32(strCount);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top