Articles in categories

Articles

Read-Write to config file

Here's how to Read and Write to the applications config file, app.config.

Add new setting:

public static void AddNewConfigsetting(string key,string value)
{
   Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
   conf.AppSettings.Settings.Add(key, value);
   conf.Save(ConfigurationSaveMode.Modified);
   ConfigurationManager.RefreshSection("appSettings");
}

Update existing setting:

public static void UpdateConfigsetting(string key,string value)
{
   Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
   conf.AppSettings.Settings.Remove(key);
   conf.AppSettings.Settings.Add(key, value);
   conf.Save(ConfigurationSaveMode.Modified);
   ConfigurationManager.RefreshSection("appSettings");
}

Read setting:

public static string ReadAppSetting(string key)
{
   return ConfigurationManager.AppSettings[key];	
}

Read all settings:

public static void ReadAllKeys()
{
   foreach (var key in ConfigurationManager.AppSettings.AllKeys)
   {
      string value = ConfigurationManager.AppSettings.Get(key);
      Console.WriteLine("Key: {0} || Value: {1}", key, value);
   }
}

Full console app test code:

namespace ConsoleApplicationConfig
{
    class Program
    {
        static void Main(string[] args)
        {
            // add new keys
            AddNewConfigsetting("TEST", "SomeValue");
            AddNewConfigsetting("TEST1", "SomeValue1");
            AddNewConfigsetting("TEST2", "SomeValue2");

            // update existing key
            UpdateConfigsetting("TEST", "NEWVALUE");

            // read all keys
            ReadAllKeys();

            Console.ReadLine();
        }

        public static void ReadAllKeys()
        {
            foreach (var key in ConfigurationManager.AppSettings.AllKeys)
            {
                string value = ConfigurationManager.AppSettings.Get(key);
                Console.WriteLine("Key: {0} || Value: {1}", key, value);
            }
        }

        public static void AddNewConfigsetting(string key, string value)
        {
            Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            conf.AppSettings.Settings.Add(key, value);
            conf.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

        public static void UpdateConfigsetting(string key, string value)
        {
            Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            conf.AppSettings.Settings.Remove(key);
            conf.AppSettings.Settings.Add(key, value);
            conf.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

        public static string ReadAppSetting(string key)
        {
            return ConfigurationManager.AppSettings.Get(key);
        }
    }
}

That's it...