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();
        } 
  • 相关阅读:
    Redis 学习之路 (009)
    树莓派进阶之路 (012)
    树莓派进阶之路 (011)
    树莓派UFW防火墙简单设置
    树莓派进阶之路 (002)
    Redis学习之路(008)- Redis C语言客户端库hiredis文档翻译
    Redis学习之路(007)- Redis学习手册(实例代码)
    树莓派进阶之路 (000)
    Redis学习之路(006)- Redis学习手册(Hashes数据类型)
    Redis学习之路(005)- redis内存数据库C客户端hiredis API 中文说明
  • 原文地址:https://www.cnblogs.com/amadeuslee/p/3744496.html
Copyright © 2011-2022 走看看