• c# asp.net 修改webconfig文件 配置


    #region 修改config文件  
    /// <summary>  
    /// 修改config文件(AppSetting节点)  
    /// </summary>  
    /// <param name="key"></param>  
    /// <param name="value">要修改成的值</param>  
    public static void UpdateAppSetting(string key, string value)  
    {  
        XmlDocument doc = new XmlDocument();  
        //获得配置文件的全路径   
        string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + "Web.config";  
      //去除文件的只读属性
        System.IO.File.SetAttributes(strFileName, System.IO.FileAttributes.Normal);
        doc.Load(strFileName);  
        //找出名称为“add”的所有元素   
        XmlNodeList nodes = doc.GetElementsByTagName("add");  
        for (int i = 0; i < nodes.Count; i++)  
        {  
            //获得将当前元素的key属性   
            XmlAttribute _key = nodes[i].Attributes["key"];  
            //根据元素的第一个属性来判断当前的元素是不是目标元素   
            if (_key != null)  
            {  
                if (_key.Value == key)  
                {  
                    //对目标元素中的第二个属性赋值   
                    _key = nodes[i].Attributes["value"];  
      
                    _key.Value = value;  
                    break;  
                }  
            }  
        }  
        //保存上面的修改   
        doc.Save(strFileName);  
    }  
      
    /// <summary>  
    /// 修改config文件(ConnectionString节点)  
    /// </summary>  
    /// <param name="name"></param>  
    /// <param name="value">要修改成的值</param>  
    public static void UpdateConnectionString(string name, string value)  
    {  
        XmlDocument doc = new XmlDocument();  
        //获得配置文件的全路径   
        string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + "Web.config";  
        //去除文件的只读属性
        System.IO.File.SetAttributes(strFileName, System.IO.FileAttributes.Normal);
        doc.Load(strFileName);  
        //找出名称为“add”的所有元素   
        XmlNodeList nodes = doc.GetElementsByTagName("add");  
        for (int i = 0; i < nodes.Count; i++)  
        {  
            //获得将当前元素的key属性   
            XmlAttribute _name = nodes[i].Attributes["name"];  
            //根据元素的第一个属性来判断当前的元素是不是目标元素   
            if (_name != null)  
            {  
                if (_name.Value == name)  
                {  
                    //对目标元素中的第二个属性赋值   
                    _name = nodes[i].Attributes["connectionString"];  
      
                    _name.Value = value;  
                    break;  
                }  
            }  
        }  
        //保存上面的修改   
        doc.Save(strFileName);  
    }  
    #endregion  
    
     
  • 相关阅读:
    phpajax高级篇
    一天学会ajax (php环境)
    php生成静态文件的方法
    MongoDB查询文档
    MongoDB删除文档
    MongoDB索引管理
    MongoDB插入文档
    MongoDB排序记录
    MongoDB 更新文档
    mongoDB 固定集合(capped collection)
  • 原文地址:https://www.cnblogs.com/dianli_jingjing/p/7595681.html
Copyright © 2020-2023  润新知