【WinForm】C#记录使用config文件读写参数的学习过程

学习想法:每一个程序都会有很多参数是需要记忆或者是保存成各个参数文件工艺配方以便快捷换型的,看到很多做法都利用conig文件的读写完成,所以这次就来学习一下使用config文件读写;

(目前使用的都是最简单的判断方式,如果有更好更简单的判断方式欢迎私信指导。)

第一步:如下做一个基本的画面,其中有Button : button1、button2、button3,TextBox : TB_TypeName、TB_TypeName1、TB_TypeName2、TB_TypeName3、textBox1、textBox2、textBox3、textBox4、textBox5、textBox6、textBox7、textBox8;其他代码上没有用到就不一一列举了;

第二步:创建config文件,默认会有一个App.config 文件,我们需要手动创建App1.config、App2.config、App3.config;

添加过程:

第三步:定义全局变量

//定义全局变量
string BT_Type1, BT_Type2, BT_Type3;

第四步:双击Form1中的“型号一”、“型号二”、“型号三”按钮,建立按钮事件“button1_Click”、“button2_Click”、“button3_Click”,并插入事件代码

        //选型按钮一点击事件
        private void button1_Click(object sender, EventArgs e)
        {
            if (BT_Type1 == "0")
            {
                BT_Type1= "1";
                button1.BackColor= Color.Green;
                BT_Type2 = "0";
                button2.BackColor = Color.Red;
                BT_Type3 = "0";
                button3.BackColor = Color.Red;
                TB_TypeName.Text = TB_TypeName1.Text;
            }
            else
            {
                BT_Type1= "0";
                button1.BackColor= Color.Red;
            }
        }

        //选型按钮二点击事件
        private void button2_Click(object sender, EventArgs e)
        {
            if (BT_Type2 == "0")
            {
                BT_Type1 = "0";
                button1.BackColor = Color.Red;
                BT_Type2 = "1";
                button2.BackColor = Color.Green;
                BT_Type3 = "0";
                button3.BackColor = Color.Red;
                TB_TypeName.Text = TB_TypeName2.Text;
            }
            else
            {
                BT_Type2 = "0";
                button2.BackColor = Color.Red;
            }
        }

        //选型按钮三点击事件
        private void button3_Click(object sender, EventArgs e)
        {
            if (BT_Type3 == "0")
            {
                BT_Type1 = "0";
                button1.BackColor = Color.Red;
                BT_Type2 = "0";
                button2.BackColor = Color.Red;
                BT_Type3 = "1";
                button3.BackColor = Color.Green;
                TB_TypeName.Text = TB_TypeName3.Text;
            }
            else
            {
                BT_Type3 = "0";
                button3.BackColor = Color.Red;
            }
        }

第五步:双击Form1窗口建立“Form1_Load”事件,并插入事件代码

private void Form1_Load(object sender, EventArgs e)
        {
            //读取config文件

            //基本数据读取
            Configuration cfa0 = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);//默认config文件
            BT_Type1 = cfa0.AppSettings.Settings["TB1T"].Value;
            BT_Type2 = cfa0.AppSettings.Settings["TB2T"].Value;
            BT_Type3 = cfa0.AppSettings.Settings["TB3T"].Value;
            TB_TypeName1.Text = cfa0.AppSettings.Settings["TB4T"].Value;
            TB_TypeName2.Text = cfa0.AppSettings.Settings["TB5T"].Value;
            TB_TypeName3.Text = cfa0.AppSettings.Settings["TB6T"].Value;

            //判断型号一按钮状态
            if (BT_Type1 == "1")
            {
                //按钮一颜色判断
                button1.BackColor = Color.Green;
                //当前型号显示判断
                TB_TypeName.Text = TB_TypeName1.Text;
                //型号一数据读取
                //实例化一个 ExeConfigurationFileMap, 并将文件的物理路径赋给其 ExeConfigFileName 属性
                ExeConfigurationFileMap Configfile = new ExeConfigurationFileMap();
                //将要打开的 Config 文件的 物理路径 赋值给 exeConfigurationFileMap 的对象的 ExeConfigFileName 属性
                Configfile.ExeConfigFilename = @"D:\参数保存测试20230128\参数保存测试20230128\App1.config";
                //调用exeConfigurationFileMap 来实例化 Configuration 对象
                Configuration cfa1 = ConfigurationManager.OpenMappedExeConfiguration(Configfile, ConfigurationUserLevel.None);
                textBox1.Text = cfa1.AppSettings.Settings["TB0T"].Value;
                textBox2.Text = cfa1.AppSettings.Settings["TB1T"].Value;
                textBox3.Text = cfa1.AppSettings.Settings["TB2T"].Value;
                textBox4.Text = cfa1.AppSettings.Settings["TB3T"].Value;
                textBox5.Text = cfa1.AppSettings.Settings["TB4T"].Value;
                textBox6.Text = cfa1.AppSettings.Settings["TB5T"].Value;
                textBox7.Text = cfa1.AppSettings.Settings["TB6T"].Value;
                textBox8.Text = cfa1.AppSettings.Settings["TB7T"].Value;
            }
            else
            {
                button1.BackColor = Color.Red;
            }
            //判断型号二按钮状态
            if (BT_Type2 == "1")
            {
                //按钮二颜色判断
                button2.BackColor = Color.Green;
                //当前型号显示判断
                TB_TypeName.Text = TB_TypeName2.Text;
                //型号二数据读取
                //实例化一个 ExeConfigurationFileMap, 并将文件的物理路径赋给其 ExeConfigFileName 属性
                ExeConfigurationFileMap Configfile = new ExeConfigurationFileMap();
                //将要打开的 Config 文件的 物理路径 赋值给 exeConfigurationFileMap 的对象的 ExeConfigFileName 属性
                Configfile.ExeConfigFilename = @"D:\参数保存测试20230128\参数保存测试20230128\App2.config";
                //调用exeConfigurationFileMap 来实例化 Configuration 对象
                Configuration cfa2 = ConfigurationManager.OpenMappedExeConfiguration(Configfile, ConfigurationUserLevel.None);
                textBox1.Text = cfa2.AppSettings.Settings["TB0T"].Value;
                textBox2.Text = cfa2.AppSettings.Settings["TB1T"].Value;
                textBox3.Text = cfa2.AppSettings.Settings["TB2T"].Value;
                textBox4.Text = cfa2.AppSettings.Settings["TB3T"].Value;
                textBox5.Text = cfa2.AppSettings.Settings["TB4T"].Value;
                textBox6.Text = cfa2.AppSettings.Settings["TB5T"].Value;
                textBox7.Text = cfa2.AppSettings.Settings["TB6T"].Value;
                textBox8.Text = cfa2.AppSettings.Settings["TB7T"].Value;
            }
            else
            {
                button2.BackColor = Color.Red;
            }
            //判断型号三按钮状态
            if (BT_Type3 == "1")
            {
                //按钮三颜色判断
                button3.BackColor = Color.Green;
                //当前型号显示判断
                TB_TypeName.Text = TB_TypeName3.Text;
                //型号三数据读取
                //实例化一个 ExeConfigurationFileMap, 并将文件的物理路径赋给其 ExeConfigFileName 属性
                ExeConfigurationFileMap Configfile = new ExeConfigurationFileMap();
                //将要打开的 Config 文件的 物理路径 赋值给 exeConfigurationFileMap 的对象的 ExeConfigFileName 属性
                Configfile.ExeConfigFilename = @"D:\参数保存测试20230128\参数保存测试20230128\App3.config";
                //调用exeConfigurationFileMap 来实例化 Configuration 对象
                Configuration cfa3 = ConfigurationManager.OpenMappedExeConfiguration(Configfile, ConfigurationUserLevel.None);
                textBox1.Text = cfa3.AppSettings.Settings["TB0T"].Value;
                textBox2.Text = cfa3.AppSettings.Settings["TB1T"].Value;
                textBox3.Text = cfa3.AppSettings.Settings["TB2T"].Value;
                textBox4.Text = cfa3.AppSettings.Settings["TB3T"].Value;
                textBox5.Text = cfa3.AppSettings.Settings["TB4T"].Value;
                textBox6.Text = cfa3.AppSettings.Settings["TB5T"].Value;
                textBox7.Text = cfa3.AppSettings.Settings["TB6T"].Value;
                textBox8.Text = cfa3.AppSettings.Settings["TB7T"].Value;
            }
            else
            {
                button3.BackColor = Color.Red;
            }
        }

第六步:建立Form1 “Form1_FormClosing”事件,并插入事件代码

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //保存config文件

            //基本数据保存
            Configuration cfa0 = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);//默认config文件
            cfa0.AppSettings.Settings["TB1T"].Value = BT_Type1;
            cfa0.AppSettings.Settings["TB2T"].Value = BT_Type2;
            cfa0.AppSettings.Settings["TB3T"].Value = BT_Type3;
            cfa0.AppSettings.Settings["TB4T"].Value = TB_TypeName1.Text;
            cfa0.AppSettings.Settings["TB5T"].Value = TB_TypeName2.Text;
            cfa0.AppSettings.Settings["TB6T"].Value = TB_TypeName3.Text;
            cfa0.Save(ConfigurationSaveMode.Modified);

            //判断型号一按钮状态
            if (BT_Type1 == "1")
            {
                //型号一数据保存
                //实例化一个 ExeConfigurationFileMap, 并将文件的物理路径赋给其 ExeConfigFileName 属性
                ExeConfigurationFileMap Configfile = new ExeConfigurationFileMap();
                //将要打开的 Config 文件的 物理路径 赋值给 exeConfigurationFileMap 的对象的 ExeConfigFileName 属性
                Configfile.ExeConfigFilename = @"D:\参数保存测试20230128\参数保存测试20230128\App1.config";
                //调用exeConfigurationFileMap 来实例化 Configuration 对象
                Configuration cfa1 = ConfigurationManager.OpenMappedExeConfiguration(Configfile, ConfigurationUserLevel.None);
                cfa1.AppSettings.Settings["TB0T"].Value = textBox1.Text;
                cfa1.AppSettings.Settings["TB1T"].Value = textBox2.Text;
                cfa1.AppSettings.Settings["TB2T"].Value = textBox3.Text;
                cfa1.AppSettings.Settings["TB3T"].Value = textBox4.Text;
                cfa1.AppSettings.Settings["TB4T"].Value = textBox5.Text;
                cfa1.AppSettings.Settings["TB5T"].Value = textBox6.Text;
                cfa1.AppSettings.Settings["TB6T"].Value = textBox7.Text;
                cfa1.AppSettings.Settings["TB7T"].Value = textBox8.Text;
                cfa1.Save(ConfigurationSaveMode.Modified);
            }

            //判断型号二按钮状态
            if (BT_Type2 == "1")
            {
                //型号二数据保存
                //实例化一个 ExeConfigurationFileMap, 并将文件的物理路径赋给其 ExeConfigFileName 属性
                ExeConfigurationFileMap Configfile = new ExeConfigurationFileMap();
                //将要打开的 Config 文件的 物理路径 赋值给 exeConfigurationFileMap 的对象的 ExeConfigFileName 属性
                Configfile.ExeConfigFilename = @"D:\参数保存测试20230128\参数保存测试20230128\App2.config";
                //调用exeConfigurationFileMap 来实例化 Configuration 对象
                Configuration cfa2 = ConfigurationManager.OpenMappedExeConfiguration(Configfile, ConfigurationUserLevel.None);
                cfa2.AppSettings.Settings["TB0T"].Value = textBox1.Text;
                cfa2.AppSettings.Settings["TB1T"].Value = textBox2.Text;
                cfa2.AppSettings.Settings["TB2T"].Value = textBox3.Text;
                cfa2.AppSettings.Settings["TB3T"].Value = textBox4.Text;
                cfa2.AppSettings.Settings["TB4T"].Value = textBox5.Text;
                cfa2.AppSettings.Settings["TB5T"].Value = textBox6.Text;
                cfa2.AppSettings.Settings["TB6T"].Value = textBox7.Text;
                cfa2.AppSettings.Settings["TB7T"].Value = textBox8.Text;
                cfa2.Save(ConfigurationSaveMode.Modified);
            }

            //判断型号三按钮状态
            if (BT_Type3 == "1")
            {
                //型号三数据保存
                //实例化一个 ExeConfigurationFileMap, 并将文件的物理路径赋给其 ExeConfigFileName 属性
                ExeConfigurationFileMap Configfile = new ExeConfigurationFileMap();
                //将要打开的 Config 文件的 物理路径 赋值给 exeConfigurationFileMap 的对象的 ExeConfigFileName 属性
                Configfile.ExeConfigFilename = @"D:\参数保存测试20230128\参数保存测试20230128\App3.config";
                //调用exeConfigurationFileMap 来实例化 Configuration 对象
                Configuration cfa3 = ConfigurationManager.OpenMappedExeConfiguration(Configfile, ConfigurationUserLevel.None);
                cfa3.AppSettings.Settings["TB0T"].Value = textBox1.Text;
                cfa3.AppSettings.Settings["TB1T"].Value = textBox2.Text;
                cfa3.AppSettings.Settings["TB2T"].Value = textBox3.Text;
                cfa3.AppSettings.Settings["TB3T"].Value = textBox4.Text;
                cfa3.AppSettings.Settings["TB4T"].Value = textBox5.Text;
                cfa3.AppSettings.Settings["TB5T"].Value = textBox6.Text;
                cfa3.AppSettings.Settings["TB6T"].Value = textBox7.Text;
                cfa3.AppSettings.Settings["TB7T"].Value = textBox8.Text;
                cfa3.Save(ConfigurationSaveMode.Modified);
            }

            //让用户选择点击
            DialogResult result = MessageBox.Show("是否确认关闭?", "警告",
                                  MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            //判断是否取消事件
            if (result == DialogResult.No)
            {
                //取消退出
                e.Cancel = true;
            }
        }

第七步:效果展示

以上就是这次【WinForm】C#记录使用config文件读写参数的学习过程,有看到文章的友友也可以私信共同学习哦!

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值