保存程序配置的方法:保存到xml文件里 - C#

    技术2025-01-16  15

    准备:

    新建一个文本文档(.txt)修改扩展名为.xml,用记事本打开写入变量,格式如下:

    <?xml version="1.0" standalone="yes"?> <NewDataSet> <Settings> <text></text> <R>255</R> <G>255</G> <B>255</B> </Settings> </NewDataSet>

    定义:

    DataSet ds; //来存放数据 string path; //XML文件路径

    读取方法:

    private void Form1_Activated(object sender, EventArgs e) { path = Application.StartupPath+"//Settings.xml"; ds = new DataSet(); ds.ReadXml(path); //ds以表格形式读取xml的数据 textBox1.Text = ds.Tables[0].Rows[0][0].ToString(); textBox1.BackColor =Color.FromArgb(Convert.ToInt16(ds.Tables[0].Rows[0][1]) ,Convert.ToInt16(ds.Tables[0].Rows[0][2]) , Convert.ToInt16(ds.Tables[0].Rows[0][3])); Activated -= new EventHandler(Form1_Activated); }

    保存方法:

    private void Form1_FormClosing(object sender, FormClosingEventArgs e) { ds.Tables[0].Rows[0][0] = textBox1.Text; ds.Tables[0].Rows[0][1] = textBox1.BackColor.R; ds.Tables[0].Rows[0][2] = textBox1.BackColor.G; ds.Tables[0].Rows[0][3] = textBox1.BackColor.B; ds.WriteXml(path); }

    注意:dataset可读入多个表,在

    <?xml version="1.0" standalone="yes"?> <NewDataSet> <Settings> <text></text> <R>255</R> <G>255</G> <B>255</B> </Settings> <Settings2> <text></text> <R>255</R> <G>255</G> <B>255</B> </Settings2> <Settings2> <text></text> <R>255</R> <G>255</G> <B>255</B> </Settings2> </NewDataSet>

     

    中Settings与Settings1是dataset中的两个Table名,Settings1表有两行,text,R,G,B,为列名.

    相关资料:

    http://yuyingying1986.blog.hexun.com/32786111_d.html

    http://hi.baidu.com/chen_yingxiang/blog/item/930c5552a954ca030df3e3e4.html

    http://blog.csdn.net/hfzsjz/archive/2009/04/11/4065048.aspx

    http://hi.baidu.com/anmor/blog/item/baeced88ad8c7a92a5c27207.html

    最新回复(0)