Lieven Cardoen Lieven Cardoen - 25 days ago 11
C# Question

Is it possible (and how) to add ConnectionString to app.config at runtime in C#?

I have a console application which gets the connectionstring as a parameter. I would have to set a ConnectionString in app.config with name 'ConnectionString' and the given parameter as the sql connectionstring.

Thx to answers. With help of the links I got to this:


var config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var connectionStringSettings =
new ConnectionStringSettings("ConnectionString",
_arguments["connectionString"], "System.Data.SqlClient");
config.ConnectionStrings.ConnectionStrings.Add(connectionStringSettings);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");

Answer

IGNORE THIS ANSWER AND SEE BELOW....

Yes - have a look at this.

This method is probably what you are after

System.Configuration.ConfigurationManager.ConnectionStrings.Add(new ConnectionStringSettings("new name", "new string");