zoukankan      html  css  js  c++  java
  • 基于.net 的加载自定义配置-误操作

    有时候 需要 将程序加载自定义的配置文件,除了自己写解析xml文件。内置的ConfigutionManager对象 是个不错的选项。

    按照 app.config 的方式,做一个副本。然后从你的配置文件中,加载指定的配置 键值对!

     //方法1-可以读取任意位置的xml文件 
    ExeConfigurationFileMap filemap = new ExeConfigurationFileMap(); filemap.ExeConfigFilename = QuartzConfigPath; //filePath; var config = ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None); // 方法2 -只能读取当前应用程序域中 根目录下的文件:注意exe的含义 //var config = ConfigurationManager.OpenExeConfiguration(QuartzConfigPath);

      

    [csharp] view plain copy
    public class ConfigureAppConfig  
    {  
    //静态构造,不能实例化  
    static ConfigureAppConfig() { } /**//// <summary>  
    /// 获取AppSettings配置节中的Key值  
    /// </summary>  
    /// <param name="keyName">Key's name</param>  
    /// <returns>Key's value</returns>  
    public static string GetAppSettingsKeyValue(string keyName)  
    {  
    return ConfigurationManager.AppSettings.Get(keyName);  
    } /**//// <summary>  
    /// 获取ConnectionStrings配置节中的值  
    /// </summary>  
    /// <returns></returns>  
    public static string GetConnectionStringsElementValue()  
    {  
    ConnectionStringSettings settings =System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"];  
    return settings.ConnectionString;  
    } /**//// <summary>  
    /// 保存节点中ConnectionStrings的子节点配置项的值  
    /// </summary>  
    /// <param name="elementValue"></param>  
    public static void ConnectionStringsSave(string ConnectionStringsName, string elementValue)  
    {  
    System.Configuration.Configuration config =  
    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
    config.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString = elementValue;  
    config.Save(ConfigurationSaveMode.Modified);  
    ConfigurationManager.RefreshSection("connectionStrings");  
    } /**//// <summary>  
    /// 判断appSettings中是否有此项  
    /// </summary>  
    private static bool AppSettingsKeyExists(string strKey, Configuration config)  
    {  
    foreach (string str in config.AppSettings.Settings.AllKeys)  
    {  
    if (str == strKey)  
    {  
    return true;  
    }  
    }  
    return false;  
    } /**//// <summary>  
    /// 保存appSettings中某key的value值  
    /// </summary>  
    /// <param name="strKey">key's name</param>  
    /// <param name="newValue">value</param>  
    public static void AppSettingsSave(string strKey, string newValue)  
    {  
    System.Configuration.Configuration config =  
     ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
    if (AppSettingsKeyExists(strKey, config))  
    {  
    config.AppSettings.Settings[strKey].Value = newValue;  
    config.Save(ConfigurationSaveMode.Modified);  
    ConfigurationManager.RefreshSection("appSettings");  
    }  
    }  
    }   
    
    // 如果你的程序是对其它程序的配置文件进行操作,代码如下:
    [csharp] view plain copy
    ExeConfigurationFileMap filemap = new ExeConfigurationFileMap();  
    filemap.ExeConfigFilename = filePath;//配置文件路径  
    config = ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);  
    if (AppSettingsKeyExists("Refresh", config))  
    {  
    config.AppSettings.Settings["Refresh"].Value = M_TimeRead.ToString();  
    } if (AppSettingsKeyExists("MachineNo", config))  
    {  
    config.AppSettings.Settings["MachineNo"].Value = M_MachineNo; }  
    config.Save(ConfigurationSaveMode.Modified);  
    ConfigurationManager.RefreshSection("appSettings");  
    config.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString = M_ConnectionString;  
    config.Save(ConfigurationSaveMode.Modified);  
    ConfigurationManager.RefreshSection("connectionStrings");   
    数据库字符串加密  
    ExeConfigurationFileMap filemap = new ExeConfigurationFileMap();  
    filemap.ExeConfigFilename = Application.ExecutablePath + ".Config"; //filePath;  
    config = ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);  
    //指定我所要的节点  
    ConfigurationSection section = config.ConnectionStrings;  
    if ((section.SectionInformation.IsProtected == false) && (section.ElementInformation.IsLocked == false))  
    {  
    //制定节点加密  
    section.SectionInformation.ProtectSection(protect);  
    //即使没有修改也保存设置  
    section.SectionInformation.ForceSave = true;  
    //配置文件内容保存到xml  
    config.Save(ConfigurationSaveMode.Full);  
    }   
    [csharp] view plain copy
     
    1. public class ConfigureAppConfig  
    2. {  
    3. //静态构造,不能实例化  
    4. static ConfigureAppConfig() { } /**//// <summary>  
    5. /// 获取AppSettings配置节中的Key值  
    6. /// </summary>  
    7. /// <param name="keyName">Key's name</param>  
    8. /// <returns>Key's value</returns>  
    9. public static string GetAppSettingsKeyValue(string keyName)  
    10. {  
    11. return ConfigurationManager.AppSettings.Get(keyName);  
    12. /**//// <summary>  
    13. /// 获取ConnectionStrings配置节中的值  
    14. /// </summary>  
    15. /// <returns></returns>  
    16. public static string GetConnectionStringsElementValue()  
    17. {  
    18. ConnectionStringSettings settings =System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"];  
    19. return settings.ConnectionString;  
    20. /**//// <summary>  
    21. /// 保存节点中ConnectionStrings的子节点配置项的值  
    22. /// </summary>  
    23. /// <param name="elementValue"></param>  
    24. public static void ConnectionStringsSave(string ConnectionStringsName, string elementValue)  
    25. {  
    26. System.Configuration.Configuration config =  
    27. ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
    28. config.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString = elementValue;  
    29. config.Save(ConfigurationSaveMode.Modified);  
    30. ConfigurationManager.RefreshSection("connectionStrings");  
    31. /**//// <summary>  
    32. /// 判断appSettings中是否有此项  
    33. /// </summary>  
    34. private static bool AppSettingsKeyExists(string strKey, Configuration config)  
    35. {  
    36. foreach (string str in config.AppSettings.Settings.AllKeys)  
    37. {  
    38. if (str == strKey)  
    39. {  
    40. return true;  
    41. }  
    42. }  
    43. return false;  
    44. /**//// <summary>  
    45. /// 保存appSettings中某key的value值  
    46. /// </summary>  
    47. /// <param name="strKey">key's name</param>  
    48. /// <param name="newValue">value</param>  
    49. public static void AppSettingsSave(string strKey, string newValue)  
    50. {  
    51. System.Configuration.Configuration config =  
    52.  ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
    53. if (AppSettingsKeyExists(strKey, config))  
    54. {  
    55. config.AppSettings.Settings[strKey].Value = newValue;  
    56. config.Save(ConfigurationSaveMode.Modified);  
    57. ConfigurationManager.RefreshSection("appSettings");  
    58. }  
    59. }  
    60. }   

     

    // 如果你的程序是对其它程序的配置文件进行操作,代码如下:

     

    [csharp] view plain copy
     
    1. ExeConfigurationFileMap filemap = new ExeConfigurationFileMap();  
    2. filemap.ExeConfigFilename = filePath;//配置文件路径  
    3. config = ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);  
    4. if (AppSettingsKeyExists("Refresh", config))  
    5. {  
    6. config.AppSettings.Settings["Refresh"].Value = M_TimeRead.ToString();  
    7. if (AppSettingsKeyExists("MachineNo", config))  
    8. {  
    9. config.AppSettings.Settings["MachineNo"].Value = M_MachineNo; }  
    10. config.Save(ConfigurationSaveMode.Modified);  
    11. ConfigurationManager.RefreshSection("appSettings");  
    12. config.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString = M_ConnectionString;  
    13. config.Save(ConfigurationSaveMode.Modified);  
    14. ConfigurationManager.RefreshSection("connectionStrings");   
    15. 数据库字符串加密  
    16. ExeConfigurationFileMap filemap = new ExeConfigurationFileMap();  
    17. filemap.ExeConfigFilename = Application.ExecutablePath + ".Config"//filePath;  
    18. config = ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);  
    19. //指定我所要的节点  
    20. ConfigurationSection section = config.ConnectionStrings;  
    21. if ((section.SectionInformation.IsProtected == false) && (section.ElementInformation.IsLocked == false))  
    22. {  
    23. //制定节点加密  
    24. section.SectionInformation.ProtectSection(protect);  
    25. //即使没有修改也保存设置  
    26. section.SectionInformation.ForceSave = true;  
    27. //配置文件内容保存到xml  
    28. config.Save(ConfigurationSaveMode.Full);  
    29. }   
  • 相关阅读:
    bite one's tongue
    你以为你以为的教育是教育吗?[转]
    使用ngnix通过uwsgi app容器部署django项目
    使用ngnix通过uwsgi app容器部署django项目
    vue作为前端的静态代码与后端融合
    linux下的下载器软件
    git的gui client终端
    java 查找bug的工具 SpotBugs 和 Findbugs
    awesome c, awesome c++
    加密货币即时交换平台 Changelly vs ShapeShift vs CoinSwitch vs ChangeNOW
  • 原文地址:https://www.cnblogs.com/micro-chen/p/7930673.html
Copyright © 2011-2022 走看看