一、前言
在項目中,我們習慣使用 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");
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
```