C#读写App.Config配置文件

C#开发WinForm程序的实际应用中,有时候会碰到软件关闭后丢失数据导致再打开软件时需要对某些固定信息进行重复录入的情况,在这里(https://www.daboke.com)教大家如何将固定信息保存到App.config配置文件中,从而避免软件关闭后数据的丢失。

我们在VS中创建一个WinForm程序,在对应的解决方案视图项目中添加新建项-应用程序配置文件,这样在我们的项目中就会有App.config的配置文件为应用程序提供相应的配置。

图1-操作指导1

使用App.config需要在项目中添加System.Configuration引用,并在主窗体代码中进行引用声明。

图2-操作指导2

打开App.config,可以看到代码框架,其中configuration是配置文件的根节点。

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

</configuration>

这里简单介绍一下常见配置文件模式:
<configuration>

<configSections>                    //配置节声明区域,包含配置节和命名空间声明

<section>                                //配置节声明

<sectionGroup>                      //定义配置节组

<section>                                //配置节组中的配置节声明

<appSettings>                        //预定义配置节

<Custom element for configuration section>  //配置节设置区域

我们经常使用的是appSettings,它是由.Net预定义的配置节可以配置任何key-value这样的键值对,在预定义的 appSettings 节中添加元素,这些元素名称都是“add”,有两个属性分别是“key”和“value”。

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <appSettings>

  <add key="name" value="张三"/>

  <add key="sex" value="男"/>

  <add key="age" value="20"/>

  <add key="department" value="研发部"/>

  <add key="phone" value="13688888888"/>

  <add key="email" value="abc@abc.com"/>

  </appSettings>

</configuration>

设计简单的窗体,将TextBox中的信息保存至App.config中,并在启动程序时读取App.config信息,从而避免软件关闭时录入的个人信息丢失。

图3-主界面

在保存配置按钮事件函数中添加App.config信息赋值的代码,即可将Textbox中的值保存到App.config中。

private void Save_Click(object sender, EventArgs e)

        {

            string file = System.Windows.Forms.Application.ExecutablePath;

            Configuration config = ConfigurationManager.OpenExeConfiguration(file);

            config.AppSettings.Settings["name"].Value = name.Text.Trim();//赋值姓名信息

            config.AppSettings.Settings["sex"].Value = sex.Text.Trim();//赋值性别信息

            config.AppSettings.Settings["age"].Value = age.Text.Trim();//赋值年龄信息

            config.AppSettings.Settings["department"].Value = department.Text.Trim();//赋值部门信息

            config.AppSettings.Settings["phone"].Value = phone.Text.Trim();//赋值电话信息

            config.AppSettings.Settings["email"].Value = email.Text.Trim();//赋值邮箱信息

            config.Save(ConfigurationSaveMode.Modified);

            ConfigurationManager.RefreshSection("appSettings");

        }

在窗体加载函数中添加读取App.config信息的代码,即可在打开软件时自动读取配置文件的信息。

private void Form1_Load(object sender, EventArgs e)

        {

            name.Text = ConfigurationManager.AppSettings["name"].ToString();//获取姓名信息

            sex.Text = ConfigurationManager.AppSettings["sex"].ToString();//获取性别信息

            age.Text = ConfigurationManager.AppSettings["age"].ToString();//获取年龄信息

            department.Text = ConfigurationManager.AppSettings["department"].ToString();//获取部门信息

            phone.Text = ConfigurationManager.AppSettings["phone"].ToString();//获取电话信息

            email.Text = ConfigurationManager.AppSettings["email"].ToString();//获取邮箱信息

        }

代码全文:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Configuration;

using System.Windows.Forms;

namespace SettingConfiguration

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void Modify_Click(object sender, EventArgs e)

        {

            name.Enabled = true;

            sex.Enabled = true;

            age.Enabled = true;

            department.Enabled = true;

            phone.Enabled = true;

            email.Enabled = true;

            MessageBox.Show("请录入个人信息!");

        }

        private void Save_Click(object sender, EventArgs e)

        {

            string file = System.Windows.Forms.Application.ExecutablePath;

            Configuration config = ConfigurationManager.OpenExeConfiguration(file);

            config.AppSettings.Settings["name"].Value = name.Text.Trim();//赋值姓名信息

            config.AppSettings.Settings["sex"].Value = sex.Text.Trim();//赋值性别信息

            config.AppSettings.Settings["age"].Value = age.Text.Trim();//赋值年龄信息

            config.AppSettings.Settings["department"].Value = department.Text.Trim();//赋值部门信息

            config.AppSettings.Settings["phone"].Value = phone.Text.Trim();//赋值电话信息

            config.AppSettings.Settings["email"].Value = email.Text.Trim();//赋值邮箱信息

            config.Save(ConfigurationSaveMode.Modified);

            ConfigurationManager.RefreshSection("appSettings");

            MessageBox.Show("个人信息已保存!");

            name.Enabled = false;

            sex.Enabled = false;

            age.Enabled = false;

            department.Enabled = false;

            phone.Enabled = false;

            email.Enabled = false;

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            name.Enabled = false;

            sex.Enabled = false;

            age.Enabled = false;

            department.Enabled = false;

            phone.Enabled = false;

            email.Enabled = false;

            name.Text = ConfigurationManager.AppSettings["name"].ToString();//获取姓名信息

            sex.Text = ConfigurationManager.AppSettings["sex"].ToString();//获取性别信息

            age.Text = ConfigurationManager.AppSettings["age"].ToString();//获取年龄信息

            department.Text = ConfigurationManager.AppSettings["department"].ToString();//获取部门信息

            phone.Text = ConfigurationManager.AppSettings["phone"].ToString();//获取电话信息

            email.Text = ConfigurationManager.AppSettings["email"].ToString();//获取邮箱信息

        }

        private void Button1_Click(object sender, EventArgs e)

        {

            System.Diagnostics.Process.Start("IEXPLORE.EXE", "https://www.daboke.com");//欢迎访问大博客!

        }

    }

}

代码链接:

https://www.daboke.com/wp-content/uploads/2020/04/SettingConfiguration.zip

原文链接:

https://www.daboke.com/program/settingconfig/

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容