public class ConfigUtils { public static string filename = System.Windows.Forms.Application.StartupPath + @"\App.config"; ////// 对[appSettings]节点依据Key值读取到Value值,返回字符串 /// /// 要读取的Key值 ///返回Value值的字符串 public static string GetAppSetting(string key) { string value = null; XmlDocument doc = new XmlDocument(); doc.Load(filename); XmlNode node = doc.SelectSingleNode("//appSettings"); XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']"); if (element != null) { value = element.GetAttribute("value"); } return value; } ////// 对[connectionStrings]节点依据name值读取到connectionString值,返回字符串 /// /// 要读取的name值 ///返回connectionString值的字符串 public static string GetConnectionString(string name) { string connectionString = null; XmlDocument doc = new XmlDocument(); doc.Load(filename); XmlNode node = doc.SelectSingleNode("//connectionStrings"); XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']"); if (element != null) { connectionString = element.GetAttribute("connectionString"); } return connectionString; } ////// 更新或新增[appSettings]节点的子节点值,存在则更新子节点Value,不存在则新增子节点,返回成功与否布尔值 /// /// 子节点Key值 /// 子节点value值 ///返回成功与否布尔值 public static bool SetAppSetting(string key, string value) { bool isSuccess = false; XmlDocument doc = new XmlDocument(); doc.Load(filename); XmlNode node = doc.SelectSingleNode("//appSettings"); try { if (node == null) { //不存在则新增appSettings子节点 XmlNode root = doc.DocumentElement; XmlElement appElement = doc.CreateElement("appSettings"); XmlElement subElement = doc.CreateElement("add"); subElement.SetAttribute("key", key); subElement.SetAttribute("value", value); appElement.AppendChild(subElement); root.AppendChild(appElement); } else { XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']"); if (element != null) { //存在则更新子节点Value element.SetAttribute("value", value); } else { //不存在则新增子节点 XmlElement subElement = doc.CreateElement("add"); subElement.SetAttribute("key", key); subElement.SetAttribute("value", value); node.AppendChild(subElement); } } using (XmlTextWriter xmlwriter = new XmlTextWriter(filename, null)) { xmlwriter.Formatting = Formatting.Indented; doc.WriteTo(xmlwriter); xmlwriter.Flush(); } isSuccess = true; } catch (Exception e) { isSuccess = false; } return isSuccess; } ////// 更新或新增[connectionStrings]节点的子节点值,存在则更新子节点,不存在则新增子节点,返回成功与否布尔值 /// /// 子节点name值 /// 子节点connectionString值 /// 子节点providerName值 ///返回成功与否布尔值 public static bool SetConnectionString(string name, string connectionString, string providerName) { bool isSuccess = false; XmlDocument doc = new XmlDocument(); doc.Load(filename); XmlNode node = doc.SelectSingleNode("//connectionStrings"); try { if (node == null) { //不存在则新增connectionStrings子节点 XmlNode root = doc.DocumentElement; XmlElement connElement = doc.CreateElement("connectionStrings"); XmlElement subElement = doc.CreateElement("add"); subElement.SetAttribute("name", name); subElement.SetAttribute("connectionString", connectionString); subElement.SetAttribute("providerName", providerName); connElement.AppendChild(subElement); root.AppendChild(connElement); } else { XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']"); if (element != null) { //存在则更新子节点 element.SetAttribute("connectionString", connectionString); element.SetAttribute("providerName", providerName); } else { //不存在则新增子节点 XmlElement subElement = doc.CreateElement("add"); subElement.SetAttribute("name", name); subElement.SetAttribute("connectionString", connectionString); subElement.SetAttribute("providerName", providerName); node.AppendChild(subElement); } } doc.Save(filename); isSuccess = true; } catch (Exception e) { isSuccess = false; } return isSuccess; } ////// 更新或新增[connectionStrings]节点的子节点值,存在则更新子节点,不存在则新增子节点,返回成功与否布尔值 /// /// 子节点name值 /// 子节点connectionString值 /// 子节点providerName值 ///返回成功与否布尔值 public static bool SetConnectionString(string name, string connectionString) { bool isSuccess = false; XmlDocument doc = new XmlDocument(); doc.Load(filename); XmlNode node = doc.SelectSingleNode("//connectionStrings"); try { if (node == null) { //不存在则新增connectionStrings子节点 XmlNode root = doc.DocumentElement; XmlElement connElement = doc.CreateElement("connectionStrings"); XmlElement subElement = doc.CreateElement("add"); subElement.SetAttribute("name", name); subElement.SetAttribute("connectionString", connectionString); connElement.AppendChild(subElement); root.AppendChild(connElement); } else { XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']"); if (element != null) { //存在则更新子节点 element.SetAttribute("connectionString", connectionString); } else { //不存在则新增子节点 XmlElement subElement = doc.CreateElement("add"); subElement.SetAttribute("name", name); subElement.SetAttribute("connectionString", connectionString); node.AppendChild(subElement); } } doc.Save(filename); isSuccess = true; } catch (Exception e) { isSuccess = false; } return isSuccess; } ////// 删除[appSettings]节点中包含Key值的子节点,返回成功与否布尔值 /// /// 要删除的子节点Key值 ///返回成功与否布尔值 public static bool DeleteAppSetting(string key) { bool isSuccess = false; XmlDocument doc = new XmlDocument(); doc.Load(filename); XmlNode node = doc.SelectSingleNode("//appSettings"); XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']"); if (element != null) { //存在则删除子节点 element.ParentNode.RemoveChild(element); } try { using (XmlTextWriter xmlwriter = new XmlTextWriter(filename, null)) { xmlwriter.Formatting = Formatting.Indented; doc.WriteTo(xmlwriter); xmlwriter.Flush(); } isSuccess = true; } catch (Exception e) { isSuccess = false; } return isSuccess; } ////// 删除[connectionStrings]节点中包含name值的子节点,返回成功与否布尔值 /// /// 要删除的子节点name值 ///返回成功与否布尔值 public static bool DeleteConnectionString(string name) { bool isSuccess = false; XmlDocument doc = new XmlDocument(); doc.Load(filename); XmlNode node = doc.SelectSingleNode("//connectionStrings"); XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']"); if (element != null) { //存在则删除子节点 node.RemoveChild(element); } try { doc.Save(filename); isSuccess = true; } catch (Exception e) { isSuccess = false; } return isSuccess; } }
class SetConfig { ////// 对[appSettings]节点依据Key值读取到Value值 /// /// ///public static string GetAppSettings(string strKey) { foreach (string key in ConfigurationManager.AppSettings) { if (key == strKey) { return ConfigurationManager.AppSettings[strKey]; } } return null; } /// /// 更新或新增[appSettings]节点的子节点值 /// /// /// public static void SetAppSettings(string newKey, string newValue) { bool isModified = false; // 如果要更改的Key已经存在 foreach (string key in ConfigurationManager.AppSettings) { if (key == newKey) { isModified = true; } } //打开 App.Config Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // 如果连接串已存在,首先删除它 if (isModified) { config.AppSettings.Settings.Remove(newKey); } //添加新的配置到配置文件里 config.AppSettings.Settings.Add(newKey, newValue); //保存对配置节所做的更改 config.Save(ConfigurationSaveMode.Modified); //强制重载配置节 ConfigurationManager.RefreshSection("appSettings"); } ////// 对[connectionStrings]节点依据name值读取 /// /// ///public static string GetConnectionString(string strName) { foreach (ConnectionStringSettings item in ConfigurationManager.ConnectionStrings) { if (item.Name == strName) { return ConfigurationManager.ConnectionStrings[strName].ConnectionString; } } return null; } /// /// 更新或新增[connectionStrings]节点的子节点值 /// /// /// public static void SetConnectionString(string newName, string newValue) { bool isModified = false; // 如果要更改的Key已经存在 foreach (ConnectionStringSettings item in ConfigurationManager.ConnectionStrings) { if (item.Name == newName) { isModified = true; } } //打开 App.Config Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // 如果连接串已存在,首先删除它 if (isModified) { config.ConnectionStrings.ConnectionStrings.Remove(newName); } //添加新的配置到配置文件里 ConnectionStringSettings conSetting = new ConnectionStringSettings(newName, newValue); config.ConnectionStrings.ConnectionStrings.Add(conSetting); //保存对配置节所做的更改 config.Save(ConfigurationSaveMode.Modified); //强制重载配置节 ConfigurationManager.RefreshSection("connectionStrings"); } }