Universal Windows(10) Apps settings is easy. Its simple to specify as part of the build, save them to storage, get them back and modify .. I mean dead easy! Smile

 

Summary

Initial App Settings.

These default settings for the app are specified in the Application class. They are used when the app is first run and saved to storage on first run. If they don't exist in storage these programmed settings are used and saved.

Subsequent App Settings

If they exist in storage they are read from there. They are easily modified and resaved in subsequent app sessions.

Reset App Settings.

Just clear them from storage and the default ones will be used for the next app instance.

 

HowTo

Initial App Settings.

You define these in the App: Application class as public static.

    sealed partial class App : Application
    {
        public static string FileName= "info.txt";
        public static string User= “fred@home.com”
        public static string Password= p@ssw0rd;

 

You then refer to them in code using the App. moniker

public string Login()
{
	string user= App.User;
	string pwd= App.Password;
	login(user,pwd);
}

This info is readonly as part of the app build. After deployment if you need to change these settings, a rebuild is required.

Subsequent App Settings

A better solution is to allow some dynamism with the app settings by storing them in the application local storage and usethem from there.

When the app is first run you copy the App.XXX setting to the app’s local storage.

The location is Windows.Storage.ApplicationData.Current.LocalSettings

Understand that Windows.Storage.ApplicationData.Current is virtual and is not available to other apps and so is secure.

       private static void ResetPassword()
        {
            string pwd= App.Password;
            SetSetting( “Password”, pwd);
        }

        private static void SetSetting(string value, string key)
        {
            var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
            localSettings.Values[key]= value;
        }

        private static string GetPassword()
        {
            if (!ApplicationData.Current.LocalSettings.Values.ContainsKey("Password"))
            {
                ResetPassword();
            }

            return (string)(ApplicationData.Current.LocalSettings.Values["Password"]);
        }

Notes:

  • In the code above, the first time the password is used, it is read from App and stored in ApplicationData.Current.LocalSettings automatically. Subsequent uses of it by the app, whether from the same runtime or later one, will use it from that storage.
  • THE DEAD EASY PART: To create OR UPDATE a setting you just write the value to the app’s  localsettings using the setting’s key.
  • The local storage is effectively a non volatile dictionary that spans app runtimes.  Unlike classic app settings, there is no need to save the settings to a file and reload them or to open the system’s registry.
  • SetSetting( ) is not specific to any string setting. The other’s might be able to be made universal for all settings using say, Reflection or the Application class (??) but its probably simplest to use specific functions for each setting.
  • The settings above are strings. Modification  for other data types is obvious.

 

ENJOY Thumbs up