vs2003下的web.config操作方法

[ 2008-04-21 10:45:21 | 作者: admin ]
字号: | |
using System;
using System.Xml;
using System.Configuration;

namespace EmanUtils

{

         /**//// <summary>
         /// 读配置文件的操作
         /// Config操作类
         /// </summary>
         public class ConfigHelper
         {
                public ConfigHelper()
                {
                     //
                     // TODO: 在此处添加构造函数逻辑
                     //
                }

                #region GetConfigValueOfKey(string Key) 得到配置文件的值

                public string GetConfigValueOfKey(string Key)
                {
                     try
                     {
                     string value = ConfigurationSettings.AppSettings[Key];
                     //Console.WriteLine(Key);
                     //Console.WriteLine(value);
                     return value;
                     }
                     catch(Exception ex)
                     {
                     Console.WriteLine(ex.Message);
                     Console.WriteLine(ex.StackTrace);
                     return "";
                     }
                }
                #endregion

// #region abstract CreateConfigValueOfKey 需要开发
// public void CreateConfigValueOfKey(string Key , string KeyValue)
// {
//
// }
// #endregion

                #region UpdateConfigValueOfKey
                public void UpdateConfigValueOfKey(string Key , string KeyValue)
                {
                     XmlDocument doc=new XmlDocument();
                     //获得配置文件的全路径
                     string strFileName=AppDomain.CurrentDomain.BaseDirectory.ToString()+"App.config";
                     Console.WriteLine(strFileName);
                     //return;
                     doc.Load(strFileName);
                     //找出名称为“add”的所有元素
                    
                     //bool IsExist = false;

                     XmlNodeList nodes=doc.GetElementsByTagName("add");
                     for(int i=0;i<nodes.Count;i++)
                     {
                     //获得将当前元素的key属性
                     XmlAttribute att=nodes[i].Attributes["key"];
                     //根据元素的第一个属性来判断当前的元素是不是目标元素
                     if (att.Value.ToString().Equals(Key))
                     {
                     //对目标元素中的第二个属性赋值
                     att=nodes[i].Attributes["value"];
                     att.Value=KeyValue;
                     //IsExist = true;
                     break;
                     }
                     }
                     //保存上面的修改
                     doc.Save(strFileName);
                }
                #endregion
         }
}
                  /// <summary>
                  /// 修改指定配置节节点信息的值
                  /// 作 者: KidYang
                  /// 日 期: 2007-03-27
                  /// </summary>
                  /// <param name="appSettingsName">给定配置节节点</param>
                  /// <param name="newValue">目标值</param>
                  public static void WriteWebConfig(string appSettingsName, string newValue)
                  {
                     string fileName = HttpContext.Current.Server.MapPath(@"~\Web.config");
                     XmlDocument xmlDoc = new XmlDocument();
                     xmlDoc.Load(fileName);
                     XmlNodeList topM = xmlDoc.DocumentElement.ChildNodes;
                     foreach (XmlElement element in topM)
                     {
                     #region 取得目标节点,并赋新值
  
                     if (element.Name == "appSettings")
                     {
                     XmlNodeList node = element.ChildNodes;
                     if (node.Count > 0)
                     {
                     foreach (XmlElement el in node)
                     {
                     if (el.Attributes["key"].Value == appSettingsName)
                     {
                     el.Attributes["value"].Value = newValue;
                     xmlDoc.Save(fileName);
                     return;
                     }
                     }
                     }
                     }
  
                     #endregion
                     }
                  }
[最后修改由 admin, 于 2008-04-21 11:02:15]
评论Feed 评论Feed: http://blog.xg98.com/feed.asp?q=comment&id=1088

这篇日志没有评论。

此日志不可发表评论。