欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c# 配置文件App.config操作類庫(kù)的方法

 更新時(shí)間:2016年12月02日 09:31:35   投稿:jingxian  
下面小編就為大家?guī)?lái)一篇c# 配置文件App.config操作類庫(kù)的方法。小編覺(jué)的挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

實(shí)例如下:

public class ConfigOperator
  {
    #region 從配置文件獲取Value
    /// <summary>
    /// 從配置文件獲取Value
    /// </summary>
    /// <param name="key">配置文件中key字符串</param>
    /// <returns></returns>
    public static string GetValueFromConfig(string key)
    {
      try
      {
        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        //獲取AppSettings的節(jié)點(diǎn) 
        AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");
        return appsection.Settings[key].Value;
      }
      catch
      {
        return "";
      }
    }
    #endregion

    #region 設(shè)置配置文件
    /// <summary>
    /// 設(shè)置配置文件
    /// </summary>
    /// <param name="key">配置文件中key字符串</param>
    /// <param name="value">配置文件中value字符串</param>
    /// <returns></returns>
    public static bool SetValueFromConfig(string key, string value)
    {
      try
      {
        //打開配置文件 
        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        //獲取AppSettings的節(jié)點(diǎn) 
        AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");
        appsection.Settings[key].Value = value;
        config.Save();

        return true;
      }
      catch
      {
        return false;
      }
    }
    #endregion

以上這篇c# 配置文件App.config操作類庫(kù)的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論