读取配置文件,web中的比较简单,在winform中的读取要注意了,实际运行时默认使用的exe.config中的配置
//web中读取web.config,需要引用System.Configuration
System.Configuration.ConnectionStringSettings connString =System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnName"];
//winform中读取app.config(其实常用的不是这个,而是下面的exe.config)
string connectionString = ConfigurationManager .ConnectionStrings[connectionName].ConnectionString
//winform中读取exe.config
string file = System.Windows.Forms.Application.ExecutablePath;
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(file);
string rtn = config.ConnectionStrings.ConnectionStrings["ConnName"].ConnectionString;
//winform中读取exe.config自定义路径名称
string configPath = ...;
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = configPath;
config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);