windowsphone7中对IsolatedStorageSettings的操作

    privateIsolatedStorageSettings setting = IsolatedStorageSettings.ApplicationSettings;//创建键值对对象
//存储键值对

public void WirteIsolatedStorageSetting(string key, object value)
{
  try
  { 

    if (!setting.Contains(key))//根据键名判断键是否存在

    {      

      setting.Add(key, value);//添加新的键值对数据
    }
    else
    {
      setting[key] = value;//重新设置已经存在的键的值
    }
    setting.Save();//永久保存键值对数据
  }
  catch (Exception e) { }
}

/// <summary>
/// 根据键值名读取相应的值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public object ReadIsolatedStorageSetting(string key)
{
  try
  {
    if (setting.Contains(key))
    {
      return setting[key];
    }
    else
    {
      return null;
    }
  }
  catch (Exception e) { return null; }
}

/// <summary>
/// 遍历键值对集合,清除键值对
/// </summary>
/// <returns></returns>
public bool ClearCacheKeys()
{
  try
  {
    foreach (string keypair in setting.Keys)//遍历键值对集合中的键,并根据键名从键值对集合中清除此键
    {      
      if (setting.Contains(keypair))
      {
        setting.Remove(keypair);
      }
    }
    setting.Save();
    return true;
  }
  catch (Exception e)
  {
    return false;
  }
}

/// <summary>
/// 获取键值
/// </summary>
/// <returns></returns>

private string KeyValues()

{

      System.Text.StringBuilder sb = new System.Text.StringBuilder("Values: ");



      foreach (Object v in settings.Values)

      {

           sb.Append(v.ToString() + "; ");

      }

      return sb.ToString();

}

你可能感兴趣的:(windows)