Home > Metro, Windows 8 > Windows 8 ApplicationSettings

Windows 8 ApplicationSettings

Every Windows 8 New UI Style App is sandboxed and by default cannot access the users filesystem.

The only locations where file access is possible, are below the install location of the app itself.

The install location can be fetched using this API:

Windows.ApplicationModel.Package.Current.InstalledLocation.Path

 

But this location is read only!

There are other locations for storing application data, local settings and roaming settings.

Roaming settings will be synced automatically to your Microsoft account and will be available on another machine when you log into this machine using the same account and start using the app there.

Syncing is not in realtime, in can take a few seconds. There is one special key, which can be used for a single setting or a composite setting. The special keyname must be "HighPriority". All settings behind this key will be synced with higher priority. This high priority setting can be a maximum of 4kb.

The maximum total size roaming settings (currently 100 kb) can be fetched using

ApplicationData.RoamingStorageQuota

This locations can be accessed using this API’s:

void DemoSettingsApi()
{
    // Get RoamingStorage Quota
    ulong roamingQuotaInKB = ApplicationData.Current.RoamingStorageQuota;

    // Write local setting
    ApplicationData.Current.LocalSettings.Values["key1"] = txtlocalKey1.Text;
    
    // Read local setting
    if (ApplicationData.Current.LocalSettings.Values["key1"] != null)
    {
        txtlocalKey1.Text = 
            ApplicationData.Current.LocalSettings.Values["key1"].ToString();
    }

    // Write roaming setting
    ApplicationData.Current.RoamingSettings.Values["key1"] = txtlocalKey1.Text;

    // Read roaming setting
    if (ApplicationData.Current.RoamingSettings.Values["key1"] != null)
    {
        txtRoamingKey1.Text = 
            ApplicationData.Current.RoamingSettings.Values["key1"].ToString();
    }

    // Write composite setting
    int zip = 0;
    Int32.TryParse(txtZipCode.Text, out zip);
    Customer c = new Customer() 
    { 
        FirstName = txtCustomerFirstname.Text, 
        LastName = txtCustomerLastname.Text, 
        ZipCode = zip 
    };

    ApplicationDataCompositeValue comp = new ApplicationDataCompositeValue();
    comp["LastName"] = c.LastName;
    comp["FirstName"] = c.FirstName;
    comp["ZipCode"] = c.ZipCode;
    ApplicationData.Current.LocalSettings.Values["CurrentCustomer"] = comp;

    // Read composite setting
    if (ApplicationData.Current.LocalSettings.Values["CurrentCustomer"] != null)
    {
        ApplicationDataCompositeValue comp2 = 
            ApplicationData.Current.LocalSettings.Values["CurrentCustomer"] as ApplicationDataCompositeValue;
        txtCustomerLastname.Text = comp2["LastName"] as string;
        txtCustomerFirstname.Text = comp2["FirstName"] as string;
        int? zipCode = comp2["ZipCode"] as int?;
        txtZipCode.Text = zipCode.ToString();
    }
}

 

 

 .

Categories: Metro, Windows 8
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: