Update app.config system.net setting at runtime

后端 未结 3 595
抹茶落季
抹茶落季 2020-11-29 04:06

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 d

3条回答
  •  鱼传尺愫
    2020-11-29 04:53

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

提交回复
热议问题