博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
App.Config操作
阅读量:7083 次
发布时间:2019-06-28

本文共 12931 字,大约阅读时间需要 43 分钟。

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"); } }

 

转载于:https://www.cnblogs.com/nepulgh/p/6736505.html

你可能感兴趣的文章
『中级篇』Docker的收费模式(53)
查看>>
上传本地项目到远程仓库
查看>>
手写Android网络框架——CatHttp(一)
查看>>
【Python实战】用Scrapyd把Scrapy爬虫一步一步部署到腾讯云上,有彩蛋
查看>>
java架构-一些设计上的基本常识
查看>>
laravel5.5 + react完成简单的CRUD
查看>>
iOS中多Target的实现
查看>>
javax.persistence.TransactionRequiredException: Executing an update/delete query
查看>>
开工大吉,推荐几个Vim神级插件
查看>>
BLOG - 个人博文系统开发总结 二:使用Lucene完成博文检索功能
查看>>
Dubbo 实践,演进及未来规划
查看>>
Android FrameWork学习(二)Android系统源码调试
查看>>
http状态码含义
查看>>
ABAP和Java SpringBoot的单元测试
查看>>
iOS节拍器开发
查看>>
扫描线
查看>>
有了这些你们团队的代码也很规范
查看>>
刷前端面经笔记(三)
查看>>
android: 高德地图
查看>>
基于快速GeoHash,如何实现海量商品与商圈的高效匹配?
查看>>