Fengzhimei@Dot.Net
Designing My Colorful Dream
    In my previous post, I get a custom key's value from config file in web/windows applications by writing a custom class. shortly afterwards, jjstar gave me a feedback, he reminded me of using System.Web.Caching.CacheDependency to do that, and I made a testing, here is the code:
1/* 
2* Return the value of the specified custom key, stored in the Web.Config file.
3* If the value has been already requested before, the function returns its 
4* cached value.
5* And it is necessary to add a dependency to the config file,
6* so that the cached value is discarded if the file is edited.
7* Example: string connString = GetConfigKeyValue("ConnString")
8*/
9public string GetConfigKeyValue(string strKey)
10{
11 string strValue = (string)HttpContext.Current.Cache[strKey];
12 //if in-memory cache is empty,get value from Web.config,and then cached
13 if (strValue == null || strValue == "")
14 {
15  strValue = ConfigurationSettings.AppSettings[strKey];
16  HttpContext.Current.Cache.Insert(strKey,strValue,new CacheDependency(Server.MapPath("Web.Config")));
17 }
18 return strValue;
19}
    jjstar, thanks for your comments
posted on 2004-05-27 14:21  fengzhimei  阅读(1699)  评论(5编辑  收藏  举报