suvish valsan
suvish valsan

Reputation: 869

Windows store app roamingsettings test if value exist

I want to know if value for key sound exist but the below code doesn't work form me

if (roamingSettings.Values["Sound"] == null)

Upvotes: 0

Views: 861

Answers (3)

Jerry Nixon
Jerry Nixon

Reputation: 31813

I think you want this

/// <summary>Returns if a setting is found in the specified storage strategy</summary>
/// <param name="key">Path of the setting in storage</param>
/// <param name="location">Location storage strategy</param>
/// <returns>Boolean: true if found, false if not found</returns>
public static bool SettingExists(string key, StorageStrategies location = StorageStrategies.Local)
{
    switch (location)
    {
        case StorageStrategies.Local:
            return Windows.Storage.ApplicationData.Current.LocalSettings.Values.ContainsKey(key);
        case StorageStrategies.Roaming:
            return Windows.Storage.ApplicationData.Current.RoamingSettings.Values.ContainsKey(key);
        default:
            throw new NotSupportedException(location.ToString());
    }
}
public enum StorageStrategies
{
    /// <summary>Local, isolated folder</summary>
    Local,
    /// <summary>Cloud, isolated folder. 100k cumulative limit.</summary>
    Roaming,
    /// <summary>Local, temporary folder (not for settings)</summary>
    Temporary
}

You would call it like this:

var _Exists = SettingExists("Sound", StorageStrategies.Roaming);

This is taken from my StorageHelper: http://codepaste.net/gtu5mq

Upvotes: 1

ZombieSheep
ZombieSheep

Reputation: 29953

"Values" is a collection, and its members may be checked like this.

if(roamingSettings.Values.ContainsKey("Sound"))
{
    var myRoamingSettingValue = roamingSettings.Values["Sound"];
    // do stuff with the value you pulled back
}
else
{
    // your roaming settings collection doesn't contain the value you are interested in.
    // Add it?
    roamingSettings.Values.Add("Sound", "myDefaultValue");
}

Upvotes: 2

Taotao
Taotao

Reputation: 168

Maybe you can try write code like this:

    if(string.IsNullOrWhiteSpace(roamingSettings.Values["Sound"]))

Upvotes: 0

Related Questions