Reputation: 2478
I'm working on a application where users should be define a output directory. After read a lot about app.config and also about ini (here says it's not recommended and .NET hasn't any class or namespace to read them) and txt (is a bit tedious) I decide to go to app.config. So I wrote this code:
// Here I save to app.config file
private void button1_Click(object sender, EventArgs e)
{
if (textBox2.Text != "")
{
oConfig.AppSettings.Settings.Add("directorio_bases", textBox2.Text);
oConfig.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
textBox1.Text = ConfigurationManager.AppSettings["directorio_bases"];
}
else
{
MessageBox.Show("Debes seleccionar una ruta donde guardar las bases generadas", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// Here I read from app.config file just for informative purpose only
private void ConfigurationUserControl_Load(object sender, EventArgs e)
{
textBox1.Text = ConfigurationManager.AppSettings["directorio_bases"];
}
// Here is where I get the path to the folder
private void button2_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
textBox2.Text = folderBrowserDialog1.SelectedPath;
}
}
And the code works fine during program execution but when I close/open the program values get lost. So my question is:
Upvotes: 0
Views: 480
Reputation: 788
Did you try to manually run the executable (e.g. from the bin folder)? If you are running the application in debug mode the app.config file is not modified.
Please have a look at the following link: C# - app config doesn't change
Upvotes: 2