前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#操作配置文件中appSettings,connectionStrings节点「建议收藏」

C#操作配置文件中appSettings,connectionStrings节点「建议收藏」

作者头像
全栈程序员站长
发布2022-08-31 17:17:44
1.1K0
发布2022-08-31 17:17:44
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

using System; using System.Configuration; using System.Web; using System.Web.Configuration;

namespace myConfiguration { #region 配置信息的操作类 /// <summary> /// 配置信息的操作 /// </summary> public class ConfigurationOperator:IDisposable { #region 变量的声明 /// <summary> /// Configuration Object /// </summary> private Configuration config; #endregion

#region 构造函数,有参数(当前应用程序的虚拟路径) /// <summary> /// 构造函数,有参数(当前应用程序的虚拟路径) /// </summary> public ConfigurationOperator() : this(HttpContext.Current.Request.ApplicationPath) {

} #endregion

#region 构造函数,有参数(其他应用程序的虚拟路径) /// <summary> /// 构造函数,有参数(其他应用程序的虚拟路径) /// </summary> /// <param name=”path”>其他应用程序的虚拟路径</param> public ConfigurationOperator(string path) { config = WebConfigurationManager.OpenWebConfiguration(path); } #endregion

#region 获取当前或其他应用程序配置文件中appSettings的所有keyName方法 /// <summary> /// 定义获取当前或其他应用程序appSettings的所有keyName方法 /// </summary> /// <returns>返回appSettings的所有keyName</returns> public string[] ActiveALLAppSettingsSection() { AppSettingsSection appSettings = (AppSettingsSection)config.GetSection(“appSettings”); string[] appKeys = appSettings.Settings.AllKeys; return appKeys; } #endregion

#region 设置当前或者其他应用程序配置文件中的appSettings节点 /// <summary> /// 定义设置当前或者其他应用程序配置文件中的appSettings节点 /// </summary> /// <param name=”key”>keyName</param> /// <param name=”value”>keyValue</param> public void SetAppSettingsSection(string key,string value) { AppSettingsSection appSettings = (AppSettingsSection)config.GetSection(“appSettings”); if (appSettings.Settings[key]!=null) { appSettings.Settings[key].Value = value; this.Save(); } else { appSettings.Settings.Add(key, value); this.Save(); } } #endregion

#region 删除当前或者其他应用程序配置文件中的appSettings节点 /// <summary> /// 定义删除当前或者其他应用程序配置文件中的appSettings节点 /// </summary> /// <param name=”key”>keyName</param> /// <returns>删除成功返回true,删除失败返回false</returns> public bool RemoveAppSettingsSection(string key) { AppSettingsSection appSettings = (AppSettingsSection)config.GetSection(“appSettings”); if (appSettings.Settings[key] != null) { appSettings.Settings.Remove(key); this.Save(); return true; } else { return false; }

} #endregion

#region 获取当前或其他应用程序配置文件中connectionStrings节点的所有ConnectionString /// <summary> /// 定义获取当前或其他应用程序配置文件中connectionStrings节点的所有ConnectionString /// </summary> /// <returns>返回connectionStrings节点的所有ConnectionString</returns> public string[] ALLConnectionStrings() { ConnectionStringsSection conSection = (ConnectionStringsSection)config.GetSection(“connectionStrings”); ConnectionStringSettingsCollection conCollection = conSection.ConnectionStrings; string[] conStrings = new string[conSection.ConnectionStrings.Count]; int i = 0; foreach (ConnectionStringSettings conSetting in conCollection) { conStrings[i++] = conSetting.ConnectionString; } return conStrings; } #endregion

#region 设置当前或其他应用程序配置文件中ConnectionString节点 /// <summary> /// 定义设置当前或其他应用程序配置文件中ConnectionString节点 /// </summary> /// <param name=”name”>connectionStrings Name</param> /// <param name=”ConnectionString”>connectionStrings ConnectionString</param> /// <param name=”providerName”>connectionStrings ProviderName</param> public void SetConnectionStringsSection(string name, string ConnectionString, string providerName) { ConnectionStringsSection conSection=(ConnectionStringsSection)config.GetSection(“connectionStrings”); if (conSection.ConnectionStrings[name] != null) { conSection.ConnectionStrings[name].ConnectionString = ConnectionString; conSection.ConnectionStrings[name].ProviderName = providerName; this.Save(); } else {

ConnectionStringSettings conSettings = new ConnectionStringSettings(name, ConnectionString, providerName); conSection.ConnectionStrings.Add(conSettings); this.Save(); } } #endregion

#region 删除当前或其他应用程序配置文件中ConnectionString节点 /// <summary> /// 定义删除当前或其他应用程序配置文件中ConnectionString节点 /// </summary> /// <param name=”name”>ConnectionStrings Name</param> /// <returns>删除成功返回true,删除失败返回false</returns> public bool RemoveConnectionStringsSection(string name) { ConnectionStringsSection conSection = (ConnectionStringsSection)config.GetSection(“connectionStrings”); if (conSection.ConnectionStrings[name] != null) { conSection.ConnectionStrings.Remove(name); this.Save(); return true; } else { return false; } } #endregion

#region 保存配置文件,并重新赋值config为null /// <summary> /// 定义保存配置文件的方法 /// </summary> public void Save() { config.Save(); config = null; } #endregion

#region 释放配置文件对象 /// <summary> /// 释放配置文件对象 /// </summary> public void Dispose() { if (config != null) { config.Save(); } } #endregion } #endregion }

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/143565.html原文链接:https://javaforall.cn

如果您是在找激活码,但输入激活码后激活失败,最新激活码地址:https://javaforall.cn/127239.html

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年5月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档