ConfigurationManager

C# ConfigurationManager 类的使用

一、前言
  在项目中,我们习惯使用 ConfigurationManager 来读取一些常量。如链接数据库字符串、一些需配置的数据(微信、QQ、支付宝)等的配置。我们需要把这些数据记录在 app.config 或者 web.config 中。接下来我们具体看一下 ConfigurationManager :

二、介绍
   命名空间:System.Configuration

程序集: System.Configuration.dll

引用:在使用中,如果出现“ 当前上下文中不存在名称:ConfigurationManager ”,你就要检查有没有引用程序集和命名空间了。

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

三、使用
  通过 AppSettings 来获取数据,简单使用案例:

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");  }  }
}

}
在文件 App.config 中配置:

add key="Setting1" value="May 5,2018"/>
add key="Setting2" value="May 5,2017"/>

2、通过使用 ConnectionStrings 来获取数据,使用案例

using System;
using System.Configuration;
using System.Data.SqlClient;  namespace ConsoleApplication1
{
class Program
{  static void Main(string[] args)  {  ReadUsers();  }  static void ReadUsers()  {  var connectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;  string queryString = "SELECT Id, Name FROM abpusers;";  using (var connection = new SqlConnection(connectionString))  {  var command = new SqlCommand(queryString, connection);  connection.Open();  using (var reader = command.ExecuteReader())  {  while (reader.Read())  {  Console.WriteLine(String.Format("{0}, {1}", reader[0], reader[1]));  }  }  }  }
}

}
在 App.config 中配置数据库链接字符串:

<add name="Default" connectionString="Server=127.0.0.1; Database=CityManage; Trusted_Connection=False; Uid=root; pwd=zxx123456;" providerName="System.Data.SqlClient" />
<add name="Abp.Redis.Cache" connectionString="localhost" />

ConfigurationManager相关推荐

  1. 古怪的ConfigurationManager类

    开始使用VS 2005,习惯性的使用ConfigurationSettings类来读取应用程序配置文件的信息时,却被编译器提示说:警告  1 "System.Configuration.Co ...

  2. .Net2.0 使用ConfigurationManager读写配置文件

    .net1.1中如果需要灵活的操作和读写配置文件并不是十分方便,一般都会在项目中封装一个配置文件管理类来进行读写操作.而在.net2.0中使用ConfigurationManager 和WebConf ...

  3. 古怪的ConfigurationManager

    用ConfigurationManager类来读取应用程序配置文件的信息时,提示:System.Configuration命名空间下找不到ConfigurationManager类, 要在该项目下手动 ...

  4. C# ConfigurationManager不存在问题解决

    在做串口通信的时候,需要使用"ConfigurationManager"类,但是添加"Using System.Configuration"命名空间后编译器依旧 ...

  5. 关于Configuration.ConfigurationManager

    Configuration.ConfigurationManager是.Net2.0的新类.在winform必须添加对System.Configuration.dll的引用才可以调用. 在做winfo ...

  6. C#中提示:当前上下文中不存在名称“ConfigurationManager”

    场景 想要在程序中获取App.config中设置的内容. 想要通过 ConfigurationManager.AppSettings[key]; 来进行获取,已经添加 using System.Con ...

  7. C# 中的 ConfigurationManager类引用方法

    c#添加了Configuration;后,竟然找不到 ConfigurationManager 这个类,后来才发现:虽然引用了using System.Configuration;这个包,但是还是不行 ...

  8. [探索 .NET 6]01 揭开 ConfigurationManager 的面纱

    在这个系列中,我将探索一下 .NET 6 中的一些新特性.已经有很多关于 .NET 6 的内容,包括很多来自 .NET 和 ASP.NET 团队本身的文章.在这个系列中,我将探索一下这些特性背后的一些 ...

  9. 细聊.NET6 ConfigurationManager的实现

    前言 友情提示:建议阅读本文之前先了解下.Net Core配置体系相关,也可以参考本人之前的文章<.Net Core Configuration源码探究 [1]>然后对.Net Core的 ...

  10. .NET 6 中的 ConfigurationManager

    .NET 6 中的 ConfigurationManager Intro .NET 6 为了 Minimal API 引入了一些新东西,其中就包含了一个全新的配置对象 ConfigurationMan ...

最新文章

  1. python考核总结_Python阶段总结
  2. vsftpd的基于pam_mysql的虚拟用户机制
  3. how to improve your ielts score on the exam?
  4. PopWindow弹出在任意位置。
  5. 微信小程序部分功能介绍和实现
  6. MFC - PreTranslateMessage()响应自定义消息
  7. gen_fsm之门禁管理
  8. std::cin 溢出_溢出:坦诚地工作与焦虑如何影响我的团队
  9. InnoDB关键特性之doublewrite
  10. hnu 暑期实训之回文串
  11. 如何删除windows服务zz 重新安装PostgreSQL时删除上次遗留service的方法
  12. python集合和序列解包
  13. 兰州大学计算机基础在线考试,[兰州大学]《计算机基础》2020年5月考试在线考核试题...
  14. 维基百科中文语料分析(附实际评论案例)
  15. 龙芯CPU芯片介绍说明
  16. 网格计算, 云计算, 集群计算, 分布式计算, 超级计算
  17. Scala-Day13
  18. linux程序图形关不了怎么办,ubuntu下卸载软件 linux关闭图形化界面
  19. 程序员必备:常见的安卓开发工具推荐
  20. 【看表情包学Linux】shell 命令及运行原理 | Linux 权限 | 文件权限的修改和转让 | 目录的权限 | Sticky bit 粘滞位

热门文章

  1. 【啊哈!算法】算法4:队列——解密QQ号
  2. 累积计数法的两个应用:骑士的金币和扑克牌魔术
  3. 百度地图添加文本标注显示文字
  4. Job定时器表达式语法说明
  5. 如何在WordPress中制作导航菜单(7个功能点)
  6. 第一卷清晨的帝国 第一百四十二章 登场
  7. Redis存储Java对象方案
  8. 图的对抗性攻击与防御
  9. android p 5t,刘作虎:一加6T出厂搭载Android P 将于11月5日发布 适配5T
  10. 当当网和1号店闹绯闻 演绎电商版“情人节”