How to update and persist a settings value

Marc Graham 21 Reputation points
2021-02-23T01:26:53.567+00:00

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?

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,821 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,203 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,611 Reputation points
    2021-02-23T02:33:05.067+00:00

    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.


  2. Karen Payne MVP 35,031 Reputation points
    2021-02-23T21:45:48.647+00:00

    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.

    0 comments No comments