盾怪网教程:是一个免费提供流行杀毒软件教程、在线学习分享的学习平台!

读取config文件的2种方法

时间:2024/12/7作者:未知来源:盾怪网教程人气:

[摘要]项目进入测试阶段,暂时闲下来了,写点笔记. 读取web.config 或者 app.config中自定义配置的值的属性,常用2种方法.假设有如下配置:<appSettings> <...
 项目进入测试阶段,暂时闲下来了,写点笔记.

读取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;
  }




关键词:读取config文件的2种办法




Copyright © 2012-2018 盾怪网教程(http://www.dunguai.com) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版