Today I lost some time trying to figure out why my Windows Phone 7 application settings got lost as soon as the application was restarted.
After several tests have concluded that the offending code was following one, which, at startup, deletes some “*.cache” files used by the application to store some temporary information.

private void ClearCache()
{
string[] cacheFiles = this.store.GetFileNames("*.cache");
foreach (string cacheFile in cacheFiles)
{
this.Delete(cacheFile);
}
}

Everything looks ok, except that the application settings are stored in a file called “__ApplicationSettings.cache” and so they were automatically deleted at startup (funny isn’t it?)

Changing the code to the following does fixes it:

private void ClearCache()
{
IEnumerable<string> cacheFiles = this.store.GetFileNames("*.cache")
.Where(file => file != "__ApplicationSettings")
.Select(file => file);
foreach (string cacheFile in cacheFiles)
{
this.Delete(cacheFile);
}
}

Next time I create temporary files I will take good care to not use a meaningful extension…
Haven’t tried but I presume the same applies to any Silverlight application.

Technorati Tags: