zoukankan      html  css  js  c++  java
  • 【转】C#读写配置文件

    用C#写了个windows服务程序,更改exe.config后,必须重新启动服务才能读取到新的配置值,如何使配置文件实时生效?

    在代码中读取配置项值之前:

    decimal.TryParse(System.Configuration.ConfigurationManager.AppSettings["StartHour"], out StartForbidHour);

    System.Configuration.ConfigurationManager.RefreshSection("appSettings");

    使得直接从磁盘读取,获得新值。

    【转载】C#读写配置文件

    http://blog.csdn.net/lanman/article/details/5287717

    读配置很简单,可以用ConfigurationManager.AppSettings[key] 来读出,

    可是写配置文件时,如果写成这样

    ConfigurationManager.AppSettings[key] = "111";

    总是提示只读,那么该怎么办呢?

    1. using System; 
    2. using System.Collections.Generic; 
    3. using System.Text; 
    4. using System.Configuration; 
    5.  
    6. namespace BQKJ.Common 
    7.     /// <summary> 
    8.     /// 对exe.Config文件中的appSettings段进行读写配置操作 
    9.     /// 注意:调试时,写操作将写在vhost.exe.config文件中 
    10.     /// </summary> 
    11.     publicclass ConfigAppSettings 
    12.     { 
    13.         /// <summary> 
    14.         /// 写入值 
    15.         /// </summary> 
    16.         /// <param name="key"></param> 
    17.         /// <param name="value"></param> 
    18.         publicstaticvoid SetValue(string key, string value) 
    19.         { 
    20.             //增加的内容写在appSettings段下 <add key="RegCode" value="0"/> 
    21.             System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
    22.             if (config.AppSettings.Settings[key] == null
    23.             { 
    24.                 config.AppSettings.Settings.Add(key, value); 
    25.             } 
    26.             else 
    27.             { 
    28.                 config.AppSettings.Settings[key].Value = value; 
    29.             } 
    30.             config.Save(ConfigurationSaveMode.Modified); 
    31.             ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件  
    32.         } 
    33.  
    34.         /// <summary> 
    35.         /// 读取指定key的值 
    36.         /// </summary> 
    37.         /// <param name="key"></param> 
    38.         /// <returns></returns> 
    39.         publicstaticstring GetValue(string key) 
    40.         {  
    41.             System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
    42.             if (config.AppSettings.Settings[key] == null
    43.                 return""
    44.             else 
    45.                 return config.AppSettings.Settings[key].Value; 
    46.         } 
    47.  
    48.     } 

    其实也很简单,用这两个封装过的方法就可以了。

    需要注意的是,在IDE调试时,写入的配置文件其实是写在了.vshost.exe.config文件中,所以你在.exe.config中是看不到的。只有直接运行exe文件时,才会正确写入到.exe.config中。

  • 相关阅读:
    轻量级前端MVVM框架avalon
    Android开发:TextView添加超链接的简便方法
    那些年一起学过的面向对象之:4 面向对象的三大特征:封装、继承、多态
    iOS 如何创建单例对象
    phing用户手册第四章Getting Started译文
    C语言中scanf/fscanf 的%[]和%n说明符的使用方法
    listview改变选中行字体颜色
    oracle数据库单个数据文件的大小限制
    古堡算式
    java反射--注解的定义与运用以及权限拦截
  • 原文地址:https://www.cnblogs.com/chshnan/p/2920709.html
Copyright © 2011-2022 走看看