I am facing problem in writing/ updating connection string in app.config file using c# codebehind . I applied different solutions but no solution could save connection string permanently in app.config. All solutions save connection string temporarily in app.config.exe which lost data when application exits.
Any help will be highly appreciated.
Regards,
Shahzad
Loading
Jignesh TrivediPosted Mar 5, 2012, 6:22 AM
In console and window application config file is emabbed with exe file.
try..
static void Main(string[] args)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory +
"..\\..\\App.config");
XmlNode appSettingsNode = xmlDoc.SelectSingleNode("configuration/appSettings");
// Attempt to locate the requested setting.
foreach (XmlNode childNode in appSettingsNode)
{
if (childNode.Attributes["key"].Value == "test")
childNode.Attributes["value"].Value = "test1";
}
xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory +
"..\\..\\App.config");
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
Console.Read();
}
only make sure application will require read/write permissions on the App.config in order to save changes.
hope this help.