您可以在这里快速查找:


 
您的位置: 编程学习 > asp编程 > 200602
文章分类

Java技术
2005: 03 04 05 06 07 08
09 10 11 12
2006: 01 02

Asp.net
2005: 07 08 09 10 11 12
2006: 01 02

VB编程
2006: 02

Asp编程
2005: 11 12
2006: 01 02

C++/VC
2005: 10 11 12
2006: 01 02

Delphi
2005: 12
2006: 01 02

其它

 本文章适合所有读者

读取config文件的两种方法

sykpboy

项目进入测试阶段,暂时闲下来了,写点笔记.

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