保存程序配置到txt文件里

以文本形式保存,很不专业的方法,但效果其实也不错

准备: 

先运行以下语句创建文件: 
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            string S = textBox1.Text + "*" + textBox1.BackColor.R + "*" + textBox1.BackColor.G + "*" + textBox1.BackColor.B;     //要写入的字符串,以'*'或其它符号隔开,以便读取时能区分
             path = Application.StartupPath + "//Settings";
            System.IO.StreamWriter swr = new System.IO.StreamWriter(new System.IO.FileStream(path, System.IO.FileMode.Create));
            swr.Write(S);
            swr.Close();
        }

读取方法: 
        private void Form1_Activated(object sender, EventArgs e)
        {
            path = Application.StartupPath + "//Settings";
            System.IO.StreamReader SR = new System.IO.StreamReader(path);
            Settings = SR.ReadToEnd().Split('*');
            SR.Close();
            //------set---------
            textBox1.Text = Settings[0];
            textBox1.BackColor = Color.FromArgb(int.Parse(Settings[1]), int.Parse(Settings[2]), int.Parse(Settings[3]));
        } 

保存方法: 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            string S = textBox1.Text + "*" + textBox1.BackColor.R + "*" + textBox1.BackColor.G + "*" + textBox1.BackColor.B;        
            System.IO.StreamWriter swr = new System.IO.StreamWriter(new System.IO.FileStream(path, System.IO.FileMode.Create));
            swr.Write(S);
            swr.Close();
        } 

注意:写入的各字符串不能包含分割字符'*'.
posted @ 2012-03-31 08:55  影子科技  阅读(350)  评论(0编辑  收藏  举报