Sunday, 13 January 2013 15:54

how to access and update appSettings in config file of windows application

Written by 
Rate this item
(0 votes)

you can put application specific settings in the
appSettings section of your app.config file in your
c# windows form application.you can access,edit,update
those settings.I show you how -

I added some settings in the app.config file -
<appSettings>
<add key="server" value="smtp.gmail.com" />
<add key="username" value="This email address is being protected from spambots. You need JavaScript enabled to view it." />
<add key="password" value="password" />
</appSettings>

then in the specific form's code,i added the following
lines to access those settings -

textBox1.Text = ConfigurationManager.AppSettings["server"];
textBox2.Text = ConfigurationManager.AppSettings["username"];
textBox3.Text = ConfigurationManager.AppSettings["password"];

then i added a button to update settings.when clicked
on the button the event method does that -

private void UpdateButton_Click_1(object sender, EventArgs e)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings["server"].Value = textBox1.Text;
config.AppSettings.Settings["username"].Value = textBox2.Text;
config.AppSettings.Settings["password"].Value = textBox3.Text;

config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

}

Read 3028 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.

Latest discussions

  • No posts to display.