• 【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();
        } 
  • 相关阅读:
    cookie和session的区别
    PHP中require和include的区别
    设计模式之建造者模式
    设计模式之抽象工厂模式
    设计模式之工厂模式
    HTTPS为什么是安全的?
    设计模式之单例模式(Singleton Pattern)
    设计模式(Design Patterns)
    Linux命令:awk
    Nginx与PHP如何协同工作
  • 原文地址:https://www.cnblogs.com/amadeuslee/p/3744496.html
Copyright © 2020-2023  润新知