using System; using System.Configuration; using System.Xml; namespace RS.Common { /// /// XML操作公共类 /// public class ConfigHelper { /// /// 获得App.xml配置内容 /// /// The key. /// public static string GetAppSettingsValue(string key) { ConfigurationManager.RefreshSection("appSettings"); return ConfigurationManager.AppSettings[key] ?? string.Empty; } /// /// 修改App.xml配置内容 /// /// The key. /// The value. /// /// 程序配置文件缺失! public static bool UpdateAppSettings(string key, string value) { var _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); if (!_config.HasFile) { throw new ArgumentException("程序配置文件缺失!"); } XmlDocument xDoc = new XmlDocument(); xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config"); XmlNode xNode; XmlElement xElemUID; xNode = xDoc.SelectSingleNode("//appSettings"); xElemUID = (XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']"); xElemUID.SetAttribute("value", value); xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config"); return true; } } }