配置文件appSettings节点和connectionStrings节点操作工具类

public class OperateConfigFile
    {
        private Configuration config;
        #region 有参构造函数
        /// <summary>
        /// 构造函数,有参数(其他应用程序的虚拟路径)
        /// </summary>
        /// <param name="path">其他应用程序的虚拟路径</param>
        public OperateConfigFile(string path)
        {
            config = WebConfigurationManager.OpenWebConfiguration(path);
        }
        #endregion
        #region 无参构造函数
        /// <summary>
        /// 构造函数,无参数
        /// </summary>
        public OperateConfigFile()
        {
            this.config = WebConfigurationManager.OpenWebConfiguration("~");
        }
        #endregion
        #region appSettings节点操作
        #region 读取appSettings节点key对应的值
        /// <summary>
        /// 读取appSettings节点key对应的值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string getValue(string key, string path)
        {
            return ConfigurationManager.AppSettings[key] ?? "";
} #endregion #region 创建或更新appSettings节点值 /// <summary> /// 创建或更新appSettings节点值 /// </summary> /// <param name="key"></param> /// <param name="value"></param> public void updateValue(string key, string value) { AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings"); //不存在就创建 if (appSettings.Settings[key] == null) { config.AppSettings.Settings.Add(key, value); } //存在就更新 else { config.AppSettings.Settings[key].Value = value; } config.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); config.Save(); return true; } else { return false; } } #endregion #endregion #region connectionStrings节点操作 #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 #endregion #region 保存配置文件,并重新赋值config为null /// <summary> /// 定义保存配置文件的方法 /// </summary> public void Save() { config.Save(); config = null; } #endregion #region 清空配置文件 /// <summary> /// 清空配置文件 /// </summary> public void clear() { this.config.AppSettings.Settings.Clear(); config.Save(); } #endregion }

所发代码为本人项目中所运用,非转载,保证质量

posted @ 2013-04-25 17:15  一杯暖咖啡  阅读(399)  评论(0)    收藏  举报