zoukankan      html  css  js  c++  java
  • 【Vegas改编】获取,更新web.config的值

      //vegas add
        public static void writeConfig(string item, string key, string value)
        {
            
    if (item == "")
            {
                item 
    = "appSettings";
            }
            Configuration  config 
    = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(System.Web.HttpContext.Current.Request.ApplicationPath);
            AppSettingsSection appSection 
    = (AppSettingsSection)config.GetSection(item);
            
    if (appSection.Settings[key] == null)
            {
                appSection.Settings.Add(key, value);
                config.Save();
            }
            
    else
            {
                appSection.Settings.Remove(key);
                appSection.Settings.Add(key, value);
                config.Save();
            }
        }



    使用web.config进行获取,更新,会丢掉session,而且更新后程序必须重新执行,所以,推荐以下方法:

        public  void SetXmlNodeValue(string key, string strValue)
        {
            
    /**************************************************************************\
            * 设置AppConfig.xml中的键值,AppConfig.xml格式如下
            <?xml version="1.0" encoding="utf-8" ?>
            <configuration>
            <appSettings>
                     <add key="ConnectionString" value=""/>
            </appSettings>
            </configuration>
            *                          key:是key上中key值
            *                          strValue:strValue是value的值
            *                                                               2006 09 14 Peter
            \*************************************************************************
    */
            
    string XPath = "/configuration/appSettings/add[@key='?']";
            System.Xml.XmlDocument domWebConfig 
    = new System.Xml.XmlDocument();
            
    string filepath = System.IO.Directory.GetCurrentDirectory() + @"\AppConfig.xml";

            domWebConfig.Load(filepath);
            System.Xml.XmlNode addKey 
    = domWebConfig.SelectSingleNode((XPath.Replace("?", key)));
            
    if (addKey == null)
            {
                
    throw new ArgumentException("没有找到<add key='" + key + "' value=/>的配置节");
            }
            addKey.Attributes[
    "value"].InnerText = strValue;
            domWebConfig.Save(filepath);
        }

        
    public  string GetXmlNodeValue(string key)
        {
            
    /**************************************************************************\
            * 获取AppConfig.xml中的键值,AppConfig.xml格式如下
            <?xml version="1.0" encoding="utf-8" ?>
            <configuration>
            <appSettings>
                     <add key="ConnectionString" value=""/>
            </appSettings>
            </configuration>
            *                          key:是key上中key值
            *                                                               2006 09 14 Peter
            \*************************************************************************
    */
            
    string XPath = "/configuration/appSettings/add[@key='?']";
            System.Xml.XmlDocument domWebConfig 
    = new System.Xml.XmlDocument();
            
    string filepath = System.IO.Directory.GetCurrentDirectory() + @"\AppConfig.xml";

            domWebConfig.Load(filepath);
            System.Xml.XmlNode addKey 
    = domWebConfig.SelectSingleNode((XPath.Replace("?", key)));
            
    if (addKey == null)
            {
                
    throw new ArgumentException("没有找到<add key='" + key + "' value=/>的配置节");
            }
            
    return addKey.Attributes["value"].InnerText.ToString();
        } 
  • 相关阅读:
    GetForegroundWindow 与 GetActiveWindow 的区别 回复 "delphier" 的问题
    给 TStringGrid 添加鼠标拖动功能 回复 "dxx" 的问题
    Delphi 的编译指令(3): 常用的预定义条件标识符
    Delphi 的编译指令(1): $DEFINE、$UNDEF、$IFDEF、$ELSE、$ENDIF
    用多媒体库 Bass.dll 播放 mp3 [17] : 如何从内存流播放 回复 "小李子子" 的问题
    Delphi 的编译指令(4): 编译指令全表(未完)
    窗口跟随 回复 "heyongan" 的问题
    字符串转换到指定格式的宽字符 回复 "厨师" 的问题
    Dll 使用 PChar 参数的小例子 回复 "linximf" 的问题
    上周热点回顾(5.286.3)
  • 原文地址:https://www.cnblogs.com/amadeuslee/p/3744496.html
Copyright © 2011-2022 走看看