读取配置文件,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);
本文详细介绍了在不同.NET环境中如何读取配置文件的方法,包括web应用中的web.config、winform应用中的app.config及exe.config,并提供了具体的代码示例。
1307

被折叠的 条评论
为什么被折叠?



