Nothing is everything.

Wednesday, December 16, 2009

How to modify the app.config programmatically.

The code snippet below shows how add a connection string during runtime:
// Open App.Config of executable
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Add an Application Setting.
config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings("myConnectionString1", @"Data Source=MyPC\SQLEXPRESS;Initial Catalog=MyDataBase2;User ID=MyUserName"));
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");

The link below contains the details:
http://geekswithblogs.net/akraus1/articles/64871.aspx

No comments:

Post a Comment

Followers