1. 程式人生 > >C#實現appSettings節點讀取與修改的方法

C#實現appSettings節點讀取與修改的方法

ffffff share 讀取 stat med modified 2-0 class exceptio

本文實例講述了C#實現appSettings節點讀取與修改的方法,分享給大家供大家參考。具體方法如下:

關鍵代碼如下:


代碼如下:

public static string GetAppSettingsValue(string key)

{

ConfigurationManager.RefreshSection(“appSettings”);

return ConfigurationManager.AppSettings[key] ?? string.Empty;

}

public static bool UpdateAppSettings(string key, string value)

{

var _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

if (!_config.HasFile)

{

throw new ArgumentException(“程序配置文件缺失!”);

}

KeyValueConfigurationElement _key = _config.AppSettings.Settings[key];

if (_key == null)

_config.AppSettings.Settings.Add(key, value);

else

_config.AppSettings.Settings[key].Value = value;

_config.Save(ConfigurationSaveMode.Modified);

return true;

}

希望本文所述對大家的C#程序設計有所幫助。

除聲明外,跑步客文章均為原創,轉載請以鏈接形式標明本文地址
C#實現appSettings節點讀取與修改的方法

本文地址: http://www.paobuke.com/develop/c-develop/pbk23438.html






相關內容

技術分享圖片C#設計模式之Template模板方法模式實現ASP.NET自定義控件 密碼強度檢測功能技術分享圖片DevExpress GridView自動滾動效果技術分享圖片C#網絡爬蟲代碼分享 C#簡單的爬取工具技術分享圖片解析C#設計模式編程中適配器模式的實現
技術分享圖片.net 隨機生成漢字技術分享圖片C#中委托用法實例分析技術分享圖片C#從文件或標準輸入設備讀取指定行的方法技術分享圖片
C#和Java中二維數組區別分析

C#實現appSettings節點讀取與修改的方法