1. 首先创建一个winform解决方案
2.右击解决方案下的项目名称—>添加—>新建项—常规->选择 “应用程序配置文件”,.net默认文件名为App.config,点击确定
打开文件app.config,加入配置文件内容,比如:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Author Name" value="Congquan Hu"/>
<add key="userName" value="0151"/>
</appSettings>
</configuration>
3. 右击解决方案下的 引用—>添加引用,选择 .net项下的System.Configuration 点击确定
4. 在程序代码中加入using System.Configuration;最后就可以用下面的代码来使用配置文件了
string name=ConfigurationSettings.AppSettings["Author Name"];
string user= ConfigurationManager.AppSettings["userName"];
private void AeroGlass_Load(object sender, EventArgs e)
{
string name = ConfigurationManager.AppSettings["Author Name"];
ConfigurationManager.AppSettings["userName"] = "Peter";
string user = ConfigurationManager.AppSettings["userName"];
MessageBox.Show("user:"+user+" author:"+name);
}
注:这里的配置文件名称是App.config,在生成应用程序时,会在应用程序的目录下重新生成一个config文件,文件名与应用程序文件名相同,后缀是.config,(比如:winform.exe的配置文件名为winform.exe.config)此时的应用程序使用的配置文件就是与它同名的config文件。