1 说明
将系统参数写到配置文件中,可避免修改参数必须重新打包程序问题。c#操作配置文件有几种方法,下面分别介绍使用App.config和INI文件方式管理。
2 使用App.config
注意:
- 程序编译后App.config会编译为“项目名.exe.config”文件。
- 程序中需要添加引用System.Configuration。
- App.config可读写数据库连接参数和用户自定义参数。
2.1 读配置信息
App.config文件
<connectionStrings>
<add name="dbConnectionString" connectionString="Data Source=10.0.1.11;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=xxx;Max Pool Size=512" providerName="System.Data.SqlClient"/>
</connectionStrings>
<appSettings>
<add key="abc" value="welcome"/>
</appSettings>
读数据库配置信息
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["dbConnect