Wednesday, 18 March 2009

Reading/Writing C# Application Settings in runtime

This is example how to access defined application settings in runtime
public static string MyStringSetting
{
  get { return Properties.Settings.Default.MyStringSetting; }
  set
  {
    Properties.Settings.Default.MyStringSetting = value;
    Properties.Settings.Default.Save();
  }
}
public static List<string> MyStringCollectionSetting
{
  get { return toStringList(Properties.Settings.Default.MyStringCollectionSetting); }
  set
  {
    Properties.Settings.Default.MyStringCollectionSetting = fromStringList(value);
    Properties.Settings.Default.Save();
  }
}
private static List<string> toStringList(StringCollection sc)
{
  List<string> list = new List<string>();
  if (sc != null)
  {
    foreach (string s in sc)
    {
      list.Add(s);
    }
  }
  return list;
}
private static StringCollection fromStringList(List<string> list)
{
  StringCollection sc = new StringCollection();
  if (list != null)
  {
    foreach (string s in list)
    {
      sc.Add(s.ToString());
    }
  }
  return sc;
}

No comments:

Post a Comment