2

I'm working on a UWP app and I need data to be stored as the setting.

I use ApplicationData.Current.LocalSettings and I store a mix of simple data and composite data in it. When my app user wants to end its session, I want all data that I stored before, to be deleted. I used ApplicationData.Current.ClearAsync(), ApplicationData.Current.LocalSettings.Values.Clear() and it seems it works but when I check settings.dat file which is where those settings are stored, I see data is still there and only their connection with their keys are cleared and made them unavailable.

The problem is from version to version there are situations that I need to change some of the keys or even stop using some of them and with this issue I described, data related to previous keys will remain in settings.dat and file size grows over time.

I need a solution to let me clear settings.dat content (or at least contents which I've written in it) completely.

TheSETJ
  • 518
  • 9
  • 25

1 Answers1

5

You are not clearing the local settings here. The ApplicationDataContainer.Values returns a PropertySet and the Clear method corresponds to the Collection class. So it clears only the collection you get and not the settings. You must use the Remove method to individually remove a setting based on a key or use an ApplicationDataContainer to store your settings. You can delete all the settings stored in the container in one go. Remove a setting individually by its key:

ApplicationData.Current.LocalSettings.Values.Remove("key");

Create ApplicationDataContainer:

var LocalSettingsContainer = ApplicationData.Current.LocalSettings;
var container = LocalSettingsContainer.CreateContainer("ContainerName", ApplicationDataCreateDisposition.Always);

Add Settings to Container:

container.Values[Key] = Value;

Delete a container:

LocalSettingsContainer.DeleteContainer("containerName");

PS: Do note that if you have any sub containers within the container you're about to delete both the settings in the specified container along with the sub containers will get deleted. More about localsettings can be found in the documentation.

Edit: You can get the Keys in the ApplicationDataContainer by casting the ApplicationDataContainer.Values to ApplicationDataContainerSettings which has Keys property by which you can remove the setting individually.

var containerSettings = (ApplicationDataContainerSettings)ApplicationData.Current.LocalSettings.Values;
var keys = containerSettings.Keys;
Razor
  • 669
  • 1
  • 10
  • 28
  • You're right about Clear function. I corrected that. – TheSETJ Sep 13 '17 at 04:24
  • What about previous keys and values? Do they remain in the file? As I've seen from multiple tests it seems new settings are appended to file so previous settings will remain (I need to mention I did not use any `ApplicationDataContainer` for previous settings). – TheSETJ Sep 13 '17 at 04:29
  • You gotta use the Remove method to delete a specific setting. – Razor Sep 13 '17 at 05:15
  • If you're not using any container to store your settings then you have to explicitly remove each setting by using the Remove method – Razor Sep 13 '17 at 05:23
  • Actually, I tried `Remove()` function in my test project. But as I mentioned the actual value will remain in the file. However, I don't know what keys are used in user devices to use them in `Remove()` function (because they had a changing part based on user info), but it seems no other solution is available for this. – TheSETJ Sep 14 '17 at 09:50
  • As I mentioned keys are based on user info. There are too many users and keys vary from user to user. – TheSETJ Sep 14 '17 at 12:08