【Vegas改编】获取,更新web.config的值

2年前 (2022) 程序员胖胖胖虎阿
245 0 0
  //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=【Vegas改编】获取,更新web.config的值/>的配置节");
        }
        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=【Vegas改编】获取,更新web.config的值/>的配置节");
        }
        
return addKey.Attributes["value"].InnerText.ToString();
    } 

 

版权声明:程序员胖胖胖虎阿 发表于 2022年10月29日 上午9:16。
转载请注明:【Vegas改编】获取,更新web.config的值 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...