有没有一个变量是可以在关闭后依旧不会更改的?比如form窗体里有个textbox1,当第一次用户打开后,输入“abcdefg”,然后点button1进行“保存”。然后关闭程序,再次打开后,点击button2(或不需要点击)就直接在textbox1显示上次输入的文字“abcdefg”?

解决方案 »

  1.   

    拿个最简单的。
    每打开1次 StatusStrip1的label1会显示:“您已使用X次”……
    这个X变量要怎么设置?
      

  2.   

    一般写到文本文件(后缀.ini)或者xml文件中
      

  3.   

    建议使用ini文件,并使用你的程序的guid值进行数据移位;如果又想不给人家那末容易该的话,可以从多方面来记录,到时候比较一下是否一致就行啦,不过会比较麻烦
      

  4.   

    给你一个序列化的代码,假设你界面有个文本框一个按钮
    在窗体加载的函数里写
      private void Form6_Load(object sender, EventArgs e)
            {            
                if (System.IO.File.Exists(Application.StartupPath + "num.dat"))
                {
                    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter =
                                               new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                    System.IO.FileStream fs = new System.IO.FileStream(Application.StartupPath + "num.dat", System.IO.FileMode.Open);
                    this.textBox1.Text = formatter.Deserialize(fs).ToString();
                    fs.Close();
                }
                else
                    this.textBox1.Text = "0";
            }
    在点击按钮保存textbox的值的函数里写
    private void button2_Click(object sender, EventArgs e)
            {
                string num = this.textBox1.Text;
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = 
                                            new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                System.IO.FileStream fs = new System.IO.FileStream(Application.StartupPath + "num.dat", System.IO.FileMode.Create);
                formatter.Serialize(fs, num);
                fs.Close();
            }
    num.dat就是一个2进制文件