修改app.config

王朝学院·作者佚名  2016-05-16
窄屏简体版  字體: 小  |  中  |  大  |  超大  

使用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);

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航