void WriteConnectionString(string name, string connectionstring) { System.Configuration.Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); cfg.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(name, connectionstring)); cfg.ConnectionStrings.SectionInformation.ForceSave = true; cfg.Save(ConfigurationSaveMode.Modified, true); ConfigurationManager.RefreshSection("connectionStrings"); }You can also use SQLDataSourceEnumerator to get a list of available SQL servers:
SqlDataSourceEnumerator sqlenum = SqlDataSourceEnumerator.Instance; DataTable SQLList = sqlenum.GetDataSources();Bear in mind that this doesn't write to the App.Config at runtime but to the [AppName].exe.Config file in the relevant bin folder.....