操作Web.config的AppSettings
  I7JaHrFMuDsU 2024年08月02日 97 0
操作Web.config的AppSettings
/// <summary>
        /// 操作Xml的AppSettings
        /// </summary>
        /// <param name="webConfigPath"></param>
        /// <param name="AppsettingsAddKey"></param>
        /// <param name="KeyValue"></param>
        /// <returns></returns>
        public static bool AppSettingsEdit(string webConfigPath, string AppsettingsAddKey, string KeyValue)
        {
            try
            {
                string path = System.Web.HttpContext.Current.Server.MapPath(webConfigPath + "/web.config");
                XmlDocument xd = new XmlDocument();
                xd.Load(path);
                //如果没有appsetting,则添加
                if (xd.SelectNodes("//appSettings").Count == 0)
                {
                    xd.DocumentElement.AppendChild(xd.CreateElement("appSettings"));
                }
                //判断节点是否存在,如果存在则修改当前节点
                bool addnode = true;
                foreach (XmlNode xn1 in xd.SelectNodes("/configuration/appSettings/add"))
                {
                    if (xn1.Attributes["key"].Value == AppsettingsAddKey)
                    {
                        addnode = false;
                        xn1.Attributes["value"].Value = KeyValue;
                        //        xn1.parentnode.removechild(xn1);
                        break;
                    }
                }
                //当前节点不存在,则添加新节点
                if (addnode)
                {
                    //创建新节点
                    XmlNode xn2 = xd.CreateElement("add");
                    //添加key
                    XmlAttribute xa = xd.CreateAttribute("key");
                    xa.Value = AppsettingsAddKey;
                    xn2.Attributes.Append(xa);
                    //添加value
                    xa = xd.CreateAttribute("value");
                    xa.Value = KeyValue;
                    xn2.Attributes.Append(xa);
                    xd.SelectSingleNode("/configuration/appSettings").AppendChild(xn2);
                }
                //保存web.config
                xd.Save(path);
                return true;
            }
            catch
            {
                return false;
            }
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2024年08月02日 0

暂无评论

推荐阅读
  I7JaHrFMuDsU   2024年08月09日   41   0   0 Stringdate
  I7JaHrFMuDsU   2024年08月02日   98   0   0 Stringxml
I7JaHrFMuDsU