Today I want to talk about where we can store application configuration in a Metro App for Windows 8. Basically, we don't have app.config file and because of this we need to "re-learn" how to store the application configuration.
The entire configuration can be stored in the application storage under "LocalSettings". The default location where we can store and retrieve the configuration data is "Windows.Storage.ApplicationData.Current.LocalSettings". In the next example we add "MaxValue" to the LocalSettings and after that retrive it:
Windows.Storage.ApplicationData.Current.LocalSettings.Values["MaxValue"] = 100;
string maxValue = Windows.Storage.ApplicationData.Current.LocalSettings.Values["MaxValue"] = 100;
Another thing that we can do with the LocaSettings is to create containers that can be used to group different settings. We can create unlimited number of containers with different settings. We cannot have two different containers with the same name, but we can have in two different containers items with the same name.
LocalSettings.CreateContainer("MyContainer", ApplicationDataCreateDisposition.Always);
LocalSettings.Containers["MyContainer"].Values["MaxValue"] = 100;
LocalSettings.CreateContainer("FooContainer", ApplicationDataCreateDisposition.Always);
LocalSettings.Containers["FooContainer"].Values["MaxValue"] = 100;
var maxValueFromMyContainer = LocalSettings.Containers["MyContainer"].Values["MaxValue"];
var maxValueFromFooContainer = LocalSettings.Containers["FooContainer"].Values["MaxValue"];
In this example I created two containers and added a configuration item.
What we should know about these configurations is that they are persisting between sessions. We need the values of them only for the first time when the application is started. After this we will be able to retrieve this data from the settings. We don't need to load this value from a configuration file and set them.
Don't forget that this functionality exists.
Comments
Post a Comment