随笔 - 750  文章 - 1  评论 - 107  阅读 - 34万

ConfigHelper.cs

复制代码
using System.Configuration;
using System.IO;

/// <summary>
/// 配置文件辅助类
/// </summary>
public class ConfigHelper
{
    /// <summary>
    /// 获得指定配置节点的值
    /// 节点不存在时返回 null
    /// </summary>
    public static string ReadAppConfig(string strKey)
    {
        if (ConfigurationManager.AppSettings[strKey] == null)
            return null;
        return ConfigurationManager.AppSettings[strKey].ToString();
    }

    /// <summary>
    /// 读 ConnectionStrings 节点 ConnectionName 的连接字符串
    /// 节点不存在时返回 null
    /// </summary>
    public static string ReadConnectionStringConfig(string ConnectionName)
    {
        if (ConfigurationManager.ConnectionStrings[ConnectionName] == null)
            return null;
        return ConfigurationManager.ConnectionStrings[ConnectionName].ConnectionString.ToString();
    }

    /// <summary>
    /// 修改指定配置节点的值
    /// 节点不存在则添加
    /// </summary>
    public static void WriteAppConfig(string newKey, string newValue)
    {
        // 打开配置文件
        //Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        if (ConfigurationManager.AppSettings[newKey] != null)
            config.AppSettings.Settings.Remove(newKey);
        // 添加新的键-值对
        config.AppSettings.Settings.Add(newKey, newValue);
        // 保存对配置文件的更改
        config.Save(ConfigurationSaveMode.Minimal);
        ConfigurationManager.RefreshSection("appSettings");
    }

    /// <summary>
    /// 写 ConnectionStrings 节点 ConnectionName 的连接字符串
    /// 节点不存在则添加
    /// </summary>
    public static void WriteConnectionStringConfig(string newName, string newConString, string newProvideName)
    {
        // 打开配置文件
        //Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        if (ConfigurationManager.ConnectionStrings[newName] != null)
            config.ConnectionStrings.ConnectionStrings.Remove(newName);
        // 添加新的连接字符串
        config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(newName, newConString, newProvideName));
        // 保存对配置文件的更改
        config.Save(ConfigurationSaveMode.Minimal);
        ConfigurationManager.RefreshSection("connectionStrings");
    }

    /// <summary>
    /// 重新读取配置文件
    /// </summary>
    public static void RefreshConfigs()
    {
        ConfigurationManager.RefreshSection("connectionStrings");
        ConfigurationManager.RefreshSection("appSettings");
    }

    // 2016年12月28日17时28分42秒 6 扩展,读其它配置文件
    public static string ReadAppConfig(string filePath, string strKey)
    {
        if (File.Exists(filePath))
        {
            ExeConfigurationFileMap map = new ExeConfigurationFileMap();
            map.ExeConfigFilename = filePath;
            Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
            if (cfg.AppSettings.Settings[strKey] == null)
                return null;
            return cfg.AppSettings.Settings[strKey].Value;
        }
        throw new FileNotFoundException(filePath + " 指定文件不存在!");
    }

    public static string ReadConnectionStringConfig(string filePath, string ConnectionName)
    {
        if (File.Exists(filePath))
        {
            ExeConfigurationFileMap map = new ExeConfigurationFileMap();
            map.ExeConfigFilename = filePath;
            Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
            if (cfg.ConnectionStrings.ConnectionStrings[ConnectionName] == null)
                return null;
            return cfg.ConnectionStrings.ConnectionStrings[ConnectionName].ConnectionString;
        }
        throw new FileNotFoundException(filePath + " 指定文件不存在!");
    }

    public static void WriteAppConfig(string filePath, string newKey, string newValue)
    {
        if (File.Exists(filePath))
        {
            ExeConfigurationFileMap map = new ExeConfigurationFileMap();
            map.ExeConfigFilename = filePath;
            Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
            if (cfg.AppSettings.Settings[newKey] != null)
                cfg.AppSettings.Settings.Remove(newKey);
            cfg.AppSettings.Settings.Add(newKey, newValue);
            // 保存对配置文件的更改
            cfg.Save(ConfigurationSaveMode.Minimal);
            return;
        }
        throw new FileNotFoundException(filePath + " 指定文件不存在!");
    }

    public static void WriteConnectionStringConfig(string filePath, string newName, string newConString, string newProvideName)
    {
        if(File.Exists(filePath))
        {
            ExeConfigurationFileMap map = new ExeConfigurationFileMap();
            map.ExeConfigFilename = filePath;
            Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
            if (cfg.ConnectionStrings.ConnectionStrings[newName] != null)
                cfg.ConnectionStrings.ConnectionStrings.Remove(newName);
            cfg.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(newName, newConString, newProvideName));
            // 保存对配置文件的更改
            cfg.Save(ConfigurationSaveMode.Minimal);
            return;
        }
        throw new FileNotFoundException(filePath + " 指定文件不存在!");
    }
}
复制代码

部分内容摘自:http://www.jb51.net/article/34476.htm

posted on   z5337  阅读(578)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2013-12-28 给JAVA的eclipse IDE 在线安装 SVN插件 / 给 eclipse 添加打开所在的文件夹功能
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示