Hello
i am working on windows application in C#.now i want change connection string in app.confing through programitically using following code
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
connectionStringsSection.ConnectionStrings["AdminDBConn"].ConnectionString = "Data Source=" + serverName + ";Initial Catalog=" + DbName + ";UID=" + userName + ";password=" + Password + "";
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");
connection string save but when application is close then and again open then he picked up old connection string mean app.config does not update permantelly.
Loading

Munesh SharmaRank #174 · 13.9K reputationPosted Apr 3, 2014, 11:34 PM EDT
Jignesh TrivediRank #16 · 50.3K reputationPosted Apr 4, 2014, 12:02 AM EDT
hi,
Please refer below link
http://www.c-sharpcorner.com/forums/thread/82763/how-do-i-change-app-config.aspx
http://www.c-sharpcorner.com/Blogs/8870/
hope this will help you.
Abhay ShankerRank #375 · 6.7K reputationPosted Apr 4, 2014, 12:11 AM EDT
private static void AddDisableFlagToConfig(string value)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (ConfigurationManager.AppSettings[ProviderKey] == null)
{
config.AppSettings.Settings.Add(ProviderKey, value);
}
else
{
config.AppSettings.Settings[ProviderKey].Value = value;
}
config.Save();
ConfigurationManager.RefreshSection("appSettings");
}can also check below links
http://www.codeproject.com/Articles/14744/Read-Write-App-Config-File-with-NET