C# 在程序执行之间保存变量

C# 在程序执行之间保存变量,c#,winforms,C#,Winforms,如果这是一个愚蠢的问题,我道歉。我还是一个真正的编程初学者 我正在制作一个Windows窗体程序,其中有一个按钮,每次按下按钮都会增加一个变量 private void CmdAdd_Click(object sender, EventArgs e) { int num; num ++; LblNum.Text = (Convert.ToString(num)); }

如果这是一个愚蠢的问题,我道歉。我还是一个真正的编程初学者

我正在制作一个Windows窗体程序,其中有一个按钮,每次按下按钮都会增加一个变量

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

我希望在程序执行之间保存变量。例如,用户1按下按钮几次,直到7,然后关闭程序。然后,用户2打开程序,数字是7而不是0。

执行此操作的方法是在应用程序关闭时使用单独的文件或数据库存储数据,并在应用程序打开时读取数据。因为您只存储一个值,所以数据库可能会过于复杂,所以我建议使用单独的文件

这不是最好的解决方案,但对于初学者来说没关系,你可以将数据写入文件,并在每次打开应用程序时读取数据 像这样:

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
    sw.WriteLine(LblNum.Text);

    //Close the file
    sw.Close();
}
首先定义int-num;超出功能范围,例如,在其顶部,如下所示:

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
    sw.WriteLine(LblNum.Text);

    //Close the file
    sw.Close();
}
为了阅读,把这个放在你的表格里

//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
sr.Close();

记住这不是最好的方法,你很快就会学到更好的解决方案

您可以使用INI文件并为所需的每个变量创建一个条目。请看。

这是一个内置的机制,称为
应用程序设置

因此,您有一个具有两个标签的表单,其中
Label2
保存值。按钮
button1
增加值

转到项目设置并创建一个名为
计数器文本的
字符串
设置

现在我们需要三个表单事件处理程序

  • 加载表单时,我们希望将
    label2
    的内容与
    CounterText
    设置连接起来

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
    
        // Update the label automatically from the setting
        label2.DataBindings.Add("Text", Properties.Settings.Default, "CounterText", true, 
                                       DataSourceUpdateMode.OnPropertyChanged);
    }
    
  • 当窗体关闭时,您希望保存设置

    protected override void OnClosing(CancelEventArgs e)
    {
        base.OnClosing(e);
    
        //  Save the settings before exit
        Properties.Settings.Default.Save();
    }
    
  • 单击按钮时,您希望增加设置中的值

    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
            num++;
            // Update the setting (int->string)
            Properties.Settings.Default.CounterText = num.ToString();
            // The system will automatically update the label text.
        }
    }
    
  • 现在,每次表单运行时,它都会读取应用程序设置并正确设置文本标签的值。此外,当按下按钮并更改设置时,标签会自动更新,因为如果定义了
    数据绑定


    在后台,XML文件保存在
    %appdata%\Local\windowsformsap1\1.0.0.0\user.config
    或应用程序的任何名称下。内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <userSettings>
            <WindowsFormsApp1.Properties.Settings>
                <setting name="CounterText" serializeAs="String">
                    <value>3</value>
                </setting>
            </WindowsFormsApp1.Properties.Settings>
        </userSettings>
    </configuration>
    
    
    3.
    

    在本例中,您可以清楚地看到计数器文本的值被保存为3。程序启动时(自动)读入此文件,程序结束时(手动)更新此文件。

    首先,num是一个具有局部作用域的变量。当您退出事件处理程序时,该变量将被销毁,因此您需要了解。即使您的第一个用户也将始终看到1。然后,如果要在程序执行之间保留一个值,则需要将该值存储在磁盘上的某个位置,然后在程序再次启动时重新加载该值并重新分配变量,因此需要阅读有关Stackoverflow欢迎使用的内容。如果要保存数据,需要使用数据库、xml、json和txt。请学习这个我认为txt对初学者很好。也许你可以用一些代码来演示。就目前而言,这不是一个完整的、自足的答案。