I apologize if this a stupid question. I am still really beginner in programming.

I am making a Windows Forms program, in which is a button which increases a Variable with every button press.

        private void CmdAdd_Click(object sender, EventArgs e)
            int num;
            num ++;
            LblNum.Text = (Convert.ToString(num));

I want the Variable to be saved between executions of the program. For example user 1 presses the button a few times up to 7 and closes the program. User 2 then opens the program and the number is 7 and not 0.

4 Answers

ehsan kiani On Best Solutions

Well this is not the best solution but for the beginner it's OK, you can write your data in a file and read it every time your app opens like this:

first define int num; out of scope of function for example on top of it like this:

int num;

private void CmdAdd_Click(object sender, EventArgs e)
    num ++;
    LblNum.Text = (Convert.ToString(num));

    //Pass the filepath and filename to the StreamWriter Constructor
    StreamWriter sw = new StreamWriter("C:\\Test.txt");

    //Write a line of text

    //Close the file

and for reading put this in your form load

//Pass the file path and file name to the StreamReader constructor
StreamReader sr = new StreamReader("C:\\Test.txt");

//Read the first line of text
line = sr.ReadLine();
num= Int32.Parse(line);
//Continue to read until you reach end of file

//close the file

remember this is not the best way, you will learn the better solutions soon!

Community On

The way to do this would be using a separate file or a database to store the data at application close and to read it on application open. Because you're storing only one value, a database would be overkill so I would recommend using a separate file. You can find more information about reading and writing to a file from this question posted by another user.

John On

You can use an INI file and make an entry for each variable you need. Look at Reading/writing an INI file .

ja72 On

This is a built-in mechanism for this, called Application Settings.

So you have a form with two labels, of which Label2 holds the value. The button button1 increments the value.


Go to the project settings and create one string setting called CounterText.


Now we need three event handlers for the form.

  1. When the form loads, we want to connect the contents of label2 with the CounterText setting

    protected override void OnLoad(EventArgs e)
        // Update the label automatically from the setting
        label2.DataBindings.Add("Text", Properties.Settings.Default, "CounterText", true, 
  2. When the form closes you want to save the settings

    protected override void OnClosing(CancelEventArgs e)
        //  Save the settings before exit
  3. When the button is clicked you want to increment the value in the settings

    private void button1_Click(object sender, EventArgs e)
        // Read the setting value (string->int)
        if (int.TryParse(Properties.Settings.Default.CounterText, out int num))
            // Increment value
            // Update the setting (int->string)
            Properties.Settings.Default.CounterText = num.ToString();
            // The system will automatically update the label text.

Now every time the form runs, it will read the application settings and set the value of the text label correctly. Also when the button is pressed and the setting is changed, the label is updated automatically because if the DataBindings defined.


What happens in the background is that an XML file is saved under %appdata%\Local\WindowsFormsApp1\\user.config or whatever the name of your application is. The contents are as follows:

<?xml version="1.0" encoding="utf-8"?>
            <setting name="CounterText" serializeAs="String">

You can clearly see the value of CounterText being saved as 3 in this case. This file gets read in when the program starts (automatically) and updated when the program ends (manually).