[ACCEPTED]-Update app.config system.net setting at runtime-system.net

Accepted answer
Score: 45

I did not understand from your question 10 if you don't have access to the app.config 9 file because of your own design implementation 8 or you just weren't able to save the config 7 file, so here is a piece of code that allows 6 you to modify and save appSettings section 5 in the config file at runtime:

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

// update SaveBeforeExit
settings[-keyname-].Value = "newkeyvalue";
...
//save the file
config.Save(ConfigurationSaveMode.Modified);
//relaod the section you modified
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);

P.S the code 4 will not save the app.config file you see 3 in the solution editor, it will pdate the 2 "program_name.exe.config" file in the operation 1 forlder.

Score: 4

using System.Configuration;

    public void save_new_connection()
    {

      string ConStrng = ConfigurationManager.ConnectionStrings.ToString();
      ConnectionStringSettings conSetting = new ConnectionStringSettings();

      conSetting.ConnectionString="server=localho;UserId=root;password=mypass;database=night_anglecourier"; 
      conSetting.Name = "courier.Properties.Settings.night_anglecourierConnectionString";
      conSetting.ProviderName = "MySql.Data.MySqlClient";

      System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        ConnectionStringsSection conSettings = (ConnectionStringsSection)config.GetSection("connectionStrings");
        conSettings.ConnectionStrings.Remove(conSetting);
        conSettings.ConnectionStrings.Add(conSetting);

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


    }

0

Score: 2

with this code i have changed the connection 2 string in the application setting of the 1 config file ... hope this may help u.

string ConStrng = ConfigurationSettings.AppSettings["ConnectionString"];
            string sss = "Data Source=";
            string xxx = ";Initial Catalog=AlfalahScholarship;Integrated Security=True";
            //ConfigurationSettings.AppSetting;
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            //Get the appSettings section.
            AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
            appSettings.Settings.Remove("ConnectionString");
            appSettings.Settings.Add("ConnectionString", sss + txtServerName.Text + xxx);

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

More Related questions