C# ConfigurationManager 類的使用

一、前言

  在項目中,我們習慣使用 ConfigurationManager 來讀取一些常量。如鏈接數(shù)據(jù)庫字符串、一些需配置的數(shù)據(jù)(微信、QQ、支付寶)等的配置。我們需要把這些數(shù)據(jù)記錄在 app.config 或者 web.config 中。接下來我們具體看一下 ConfigurationManager? :

二、介紹

   命名空間:System.Configuration

   程序集: System.Configuration.dll

引用:在使用中,如果出現(xiàn)“當前上下文中不存在名稱:ConfigurationManager ”,你就要檢查有沒有引用程序集和命名空間了。

  ConfigurationManager類: 包含屬性(AppSettings、ConnectionStrings )、方法(GetSection、OpenExeConfiguration、OpenExeConfiguration、OpenMachineConfiguration、OpenMappedExeConfiguration、OpenMappedExeConfiguration、OpenMappedMachineConfiguration、RefreshSection)

三、使用

  通過 AppSettings 來獲取數(shù)據(jù),簡單使用案例:

```

using System;

using System.Configuration;

namespace ConsoleApplication1

{

? ? class Program

? ? {

? ? ? ? static void Main(string[] args)

? ? ? ? {

? ? ? ? ? ? ReadAllSettings();

? ? ? ? ? ? ReadSetting("Setting1");

? ? ? ? ? ? ReadSetting("NotValid");

? ? ? ? ? ? AddUpdateAppSettings("NewSetting", "May 7, 2014");

? ? ? ? ? ? AddUpdateAppSettings("Setting1", "May 8, 2014");

? ? ? ? ? ? ReadAllSettings();

? ? ? ? }

? ? ? ? static void ReadAllSettings()

? ? ? ? {

? ? ? ? ? ? try

? ? ? ? ? ? {

? ? ? ? ? ? ? ? var appSettings = ConfigurationManager.AppSettings;

? ? ? ? ? ? ? ? if (appSettings.Count == 0)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? Console.WriteLine("AppSettings is empty.");

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? else

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? foreach (var key in appSettings.AllKeys)

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine("Key: {0} Value: {1}", key, appSettings[key]);

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? catch (ConfigurationErrorsException)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? Console.WriteLine("Error reading app settings");

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? static void ReadSetting(string key)

? ? ? ? {

? ? ? ? ? ? try

? ? ? ? ? ? {

? ? ? ? ? ? ? ? var appSettings = ConfigurationManager.AppSettings;

? ? ? ? ? ? ? ? string result = appSettings[key] ?? "Not Found";

? ? ? ? ? ? ? ? Console.WriteLine(result);

? ? ? ? ? ? }

? ? ? ? ? ? catch (ConfigurationErrorsException)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? Console.WriteLine("Error reading app settings");

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? static void AddUpdateAppSettings(string key, string value)

? ? ? ? {

? ? ? ? ? ? try

? ? ? ? ? ? {

? ? ? ? ? ? ? ? var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

? ? ? ? ? ? ? ? var settings = configFile.AppSettings.Settings;

? ? ? ? ? ? ? ? if (settings[key] == null)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? settings.Add(key, value);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? else

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? settings[key].Value = value;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? configFile.Save(ConfigurationSaveMode.Modified);

? ? ? ? ? ? ? ? ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);

? ? ? ? ? ? }

? ? ? ? ? ? catch (ConfigurationErrorsException)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? Console.WriteLine("Error writing app settings");

? ? ? ? ? ? }

? ? ? ? }

? ? }

}

```

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容