c# 配置文件App.config操作類庫的方法
更新時間:2016年12月02日 09:31:35 投稿:jingxian
下面小編就為大家?guī)硪黄猚# 配置文件App.config操作類庫的方法。小編覺的挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
實例如下:
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é)點 AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings"); return appsection.Settings[key].Value; } catch { return ""; } } #endregion #region 設置配置文件 /// <summary> /// 設置配置文件 /// </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é)點 AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings"); appsection.Settings[key].Value = value; config.Save(); return true; } catch { return false; } } #endregion
以上這篇c# 配置文件App.config操作類庫的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
c# 連接access數(shù)據(jù)庫config配置
c# 連接access數(shù)據(jù)庫config配置,需要的朋友可以參考一下2013-02-02基于c#圖像灰度化、灰度反轉(zhuǎn)、二值化的實現(xiàn)方法詳解
本篇文章是對c#圖像灰度化、灰度反轉(zhuǎn)、二值化的實現(xiàn)方法進行了詳細的分析介紹,需要的朋友參考下2013-05-05C# 中的IComparable和IComparer的使用及區(qū)別
這篇文章主要介紹了C# 中的IComparable和IComparer的使用及區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-01-01使用代理模式來進行C#設計模式開發(fā)的基礎(chǔ)教程
這篇文章主要介紹了使用代理模式來進行C#設計模式開發(fā)的基礎(chǔ)教程,代理模式主張在客戶端和目標對象中間建立中介來降低程序設計的耦合度,需要的朋友可以參考下2016-02-02