How to change the app.config file at run time?

1

How can I change the app.config file at run time?

I want to change the connection string:

<add name="WiconEstoqueDataContextConnectionString" connectionString="User Id=USER;Password=PASS;Host=HOST;Database=wicon_estoque;Persist Security Info=True" />
    
asked by anonymous 06.03.2017 / 14:23

1 answer

4

Hello, try this:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
connectionStringsSection.ConnectionStrings["WiconEstoqueDataContextConnectionString"].ConnectionString = "sua nova conexão";
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");

I hope this can help you.

    
06.03.2017 / 14:31