question

MarcGraham-7285 avatar image
0 Votes"
MarcGraham-7285 asked ·

How to update and persist a settings value

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?

dotnet-csharpwindows-forms
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered ·

Hi MarcGraham-7285
You can update and save the settings via following code:


  Properties.Settings.Default.Test = "HI";
     Properties.Settings.Default.Save();

70885-223.png
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.



223.png (2.6 KiB)
· 3 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

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.

0 Votes 0 ·
Viorel-1 avatar image Viorel-1 MarcGraham-7285 ·

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.

0 Votes 0 ·
DanielZhang-MSFT avatar image DanielZhang-MSFT MarcGraham-7285 ·

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

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered ·

The following I've created two settings.

71282-s1.png

Two TextBox controls

71255-f1.png

Data bind via selecting a TextBox, select the following

71256-f2.png

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.





s1.png (4.4 KiB)
f1.png (3.2 KiB)
f2.png (6.6 KiB)
· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.