Update app.config system.net setting at runtime

37,252

Solution 1

I did not understand from your question if you don't have access to the app.config file because of your own design implementation or you just weren't able to save the config file, so here is a piece of code that allows you to modify and save appSettings section 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 will not save the app.config file you see in the solution editor, it will pdate the "program_name.exe.config" file in the operation forlder.

Solution 2

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);


    }

Solution 3

with this code i have changed the connection string in the application setting of the 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);
Share:
37,252

Related videos on Youtube

Enigmazflo
Author by

Enigmazflo

Updated on November 04, 2020

Comments

  • Enigmazflo
    Enigmazflo over 3 years

    I need to update a setting in the system.net SectionGroup of a .Net exe app.config file at runtime. I don't have write access to the original config file at runtime (I am developing a .Net dll add-in which is hosted in an exe provided by the app which I have no control over) so I was hoping to save a copy of the file and replace the config in the exe with the modified version at runtime. I've tried the following but it's not working. Any suggestions?

        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        NetSectionGroup netSectionGroup = config.GetSectionGroup("system.net") as NetSectionGroup;
        netSectionGroup.Settings.HttpWebRequest.UseUnsafeHeaderParsing = true;                      
        config.SaveAs(@"C:\ProgramData\test.config", ConfigurationSaveMode.Full);
    
        AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"C:\ProgramData\test.config");
    
  • Enigmazflo
    Enigmazflo almost 15 years
    I am developing an add-in for a .Net application (Windows Media Center). The addin takes the form of a dll .Net assembly that is loaded into a seperate host process (ehexthost.exe) managed by the Media Center exe (ehshell.exe). I cannot save to the the original app.config file for the host process because of it's location hence the need to save to alternative path. Also I want make a change to the system.net SectionGroup not AppSettings. Thanks.
  • Mohammad Heydari
    Mohammad Heydari about 12 years
    I can confirm that this works in .NET 4.0 as well.(Thanks @AlexDrenea !) The key (pardon the pun) for me was to have the settings like this: <configuration> <appSettings> <add key="blahblah" value="bloog" /> </appSettings> </configuration>
  • bizah
    bizah over 10 years
    Confirmed... It will not save in debug mode. It will only save if you run the .exe in the bin folder. I did not see any evidence of it saving in the "program_name.exe.config" file.