2013/07/15

Update app.config settings at runtime

using System.Configuration;

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection appSettings = config.AppSettings.Settings;

appSettings["WorkingMinutes"].Value = numericUpDownWorkingPeriod.Value.ToString();
appSettings["RestMinutes"].Value = numericUpDownRestPeriod.Value.ToString();
appSettings["PhotoPath"].Value = textBoxPhotoPath.Text.Trim();

config.Save();
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);


Same as above, different style.
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection appSettings = config.AppSettings.Settings;

appSettings["key1"].Value = "value1";
appSettings["key2"].Value = "value2";

config.Save();
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);

LINQ: Ways to have two filters.

string[] source = new string[5] { "12345", "1234", "45678", "6789", "6" };

// Method 1
var result1 = from o in source
where o.Length > 4 && o.StartsWith("1")
select o;
// Method 2
var result2 = from o in source
where (o.Length > 4 & o.StartsWith("1"))
select o;
// Method 3
var result3 = from o in source
where o.Length > 4
where o.StartsWith("1")
select o;
// Method 4
var result4 = source.Where(o => o.Length > 4).Where(o => o.StartsWith("1"));

Different style:
string[] source = new string[5] { "12345""1234""45678""6789""6" };

// Method 1
var result1 = from o in source
              where o.Length > 4 && o.StartsWith("1")
              select o;
// Method 2
var result2 = from o in source
              where (o.Length > 4 & o.StartsWith("1"))
              select o;
// Method 3
var result3 = from o in source
              where o.Length > 4
              where o.StartsWith("1")
              select o;
// Method 4
var result4 = source.Where(o => o.Length > 4).Where(o => o.StartsWith("1"));