I want to change a string value in the project settings at run time in such a way that it will persist till the next execution. It seems to be impossible. Is that correct?
I want to change a string value in the project settings at run time in such a way that it will persist till the next execution. It seems to be impossible. Is that correct?
Hi MarcGraham-7285
You can update and save the settings via following code:
Properties.Settings.Default.Test = "HI";
Properties.Settings.Default.Save();
Here is a related document.
Best Regards,
Daniel Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
This doesn't seem to work.
Properties.Settings.Default.typeList = String.Join(",", items);
Properties.Settings.Default.Save();
The value has been stored in the Properties. The property in question is user level. When the application is restarted, the change is not present. The app.config file, where the setting are stored has not been rewritten.
I am running inside VS, if that's an issue.
I also note that your example does not show the result of the code. I found that curious.
Do you mean that the value is not present in Settings window of Project Properties? I think that this is normal. Check if the value is present on next run in Properties.Settings.Default.typeList variable.
Hi @MarcGraham-7285,
>>When the application is restarted, the change is not present. The app.config file, where the setting are stored has not been rewritten.
When I restart the application and use following code to check the setting value:
MessageBox.Show(Properties.Settings.Default.Test);
The value is "HI" which means my "Test" setting is rewritten.
And as Viorel-1 said, the rewritten setting value is not present in Settings window of Project Properties.
Best Regards,
Daniel Zhang
The following I've created two settings.
Two TextBox controls
Data bind via selecting a TextBox, select the following
Code for a form with the two TextBoxes
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Closing += OnClosing;
}
private void OnClosing(object sender, CancelEventArgs e)
{
Properties.Settings.Default.Save();
}
}
}
Now if we added the following code it still works
private void button1_Click(object sender, EventArgs e)
{
Properties.Settings.Default.FirstName = "Jane";
}
If the above does not work for you there is something else unknown to us that is causing this to happen. Personally I never use Settings but instead a completely different method.
11 people are following this question.