读写系统配置文件的Key值

来源:互联网 发布:百分百采集软件 编辑:程序博客网 时间:2024/05/27 21:49

直接上代码


<pre name="code" class="csharp">/// <summary>    /// 读写系统配置xml文件。    /// </summary>    public class ConfigurationFile    {        /// <summary>        /// 写入key值        /// </summary>        public static bool SetKeyValue(string key, string value)        {            //增加的内容写在appSettings段下 <add key="RegCode" value="0"/>            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);            try            {                if (config.AppSettings.Settings[key] == null)                {                    config.AppSettings.Settings.Add(key, value);                }                else                {                    config.AppSettings.Settings[key].Value = value;                }                config.Save(ConfigurationSaveMode.Modified);                ConfigurationManager.RefreshSection("appSettings");            }            catch (Exception)            {                return false;            }            return true;        }        /// <summary>        /// 读取指定key的值        /// </summary>        public static string GetKeyValue(string key)        {            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);            if (config.AppSettings.Settings[key] == null)                return "";            else                return config.AppSettings.Settings[key].Value;        }    }




0 0
原创粉丝点击