首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

winform配置文件的简略使用

2012-08-27 
winform配置文件的简单使用configurationappSettingsadd keyAuthor Name valueSOPPER /add k

winform配置文件的简单使用

<configuration>

<appSettings>

<add key="Author Name" value="SOPPER" />

<add key="sqlconn" value="Data Source=(local);password=sa;user id=sa;Initial Catalog =db" />

</appSettings>

</configuration>

3. 右击解决方案下的 引用—>添加引用,选择 .net项下的System.Configuration 点击确定

4. 在程序代码中加入using System.Configuration;最后就可以用下面的代码来使用配置文件了

string appName =ConfigurationSettings.AppSettings["author Name"];

string strSql = ConfigurationManager.AppSettings["sqlconn"];//获取配置文件里自己设置的连接字符串

//string strSql = System.Configuration.ConfigurationManager.ConnectionStrings[0].ConnectionString;//获取系统默认的连接字符串

label1.Text = appName;

label2.Text = strSql;

注:这里的配置文件名称是app.config,在生成应用程序时,会在应用程序的目录下重新生成一个config文件,文件名与应用程序文件名相同,后缀是.config,(比如:winform.exe的配置文件名为winform.exe.config)此时的应用程序使用的配置文件就是与它同名的config文件。

热点排行