首页 > 移动平台 > 详细

使用Linq to XML 修改app.config

时间:2014-02-25 08:11:27      阅读:321      评论:0      收藏:0      [点我收藏+]

使用其他的方法修改app.config无效。而且修改的是*.vshost.exe.Config,程序运行时正常,关闭之后就还是原来的值。

bubuko.com,布布扣
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
             configuration.AppSettings.Settings["节点名称"].Value ="0";  
             configuration.Save(ConfigurationSaveMode.Modified);   
其他方法(测试无效)

直接上代码。

bubuko.com,布布扣
 //获取config路径
                string path = System.Windows.Forms.Application.ExecutablePath + ".config";
                XDocument doc = XDocument.Load(path);
                //查找所有节点
                IEnumerable<XElement> element = doc.Element("configuration").Element("appSettings").Elements();
                //遍历节点
                foreach (XElement item in element)
                {
                    if (item.Attribute("key") != null && item.Attribute("key").Value == "节点名称")
                    {
                        if (item.Attribute("value") != null)
                        {
                            item.Attribute("value").SetValue(DateTime.Now.ToString("d"));
                        }
                    }
                }
                //保存
                doc.Save(path);
bubuko.com,布布扣

使用Linq to XML 修改app.config

原文:http://www.cnblogs.com/xcong/p/3564140.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!