项目进入测试阶段,暂时闲下来了,写点笔记.
读取web.config 或者 app.config中自定义配置的值的属性,常用2种方法.
假设有如下配置:
<appSettings>
<add key="A" value="config with A"/>
<add key="B" value="config with B"/>
</appSettings>
using System.Configuration;
[A] 方法
string strTest = ConfigurationSettings.AppSettings["A"]; // get A 's value
[B] 方法
AppSettingsReader appReader = new AppSettingsReader();
string strTest = appReader.GetValue(strKey,typeof(string)).ToString();
e.g.
private string GetConfig(string strKey)
{
AppSettingsReader appReader = new AppSettingsReader();
string strReturn;
try
{
strReturn = appReader.GetValue(strKey,typeof(string)).ToString();
}
catch(Exception ex)
{
strReturn = ex.Message.ToString();
}
finally
{
appReader = null;
}
return strReturn;
}

项目进入测试阶段,博主记录笔记。介绍了读取 web.config 或 app.config 中自定义配置值属性的两种常用方法,给出了配置示例,并分别展示了两种方法的代码实现,还给出了一个获取配置值的函数示例。
3495

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



