基本信息
源码名称:C#读写App.Config配置文件
源码大小:0.19M
文件格式:.zip
开发语言:C#
更新时间:2020-04-02
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
这篇文章主要介绍了C#中读写App.config配置文件的方法,代码实例演示如何将主要信息保存至App.config中,可以减少每次启动程序时固定信息的重复录入。
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");//欢迎访问大博客!
}
}
}