首页 > 学院 > 开发设计 > 正文

使用Linq to XML 修改app.config

2019-11-17 03:20:23
字体:
来源:转载
供稿:网友

使用Linq to xml 修改app.config

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

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

直接上代码。

 //获取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);


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表