获取App.config配置文件中的参数值
下面通过代码示例给大家展示下,具体内容如下:
首先添加System.Configuration引用
向App.config配置文件添加参数
App.config添加
向App.config配置文件添加参数
例子:
在这个App.config配置文件中,我添加了4个参数,App.config参数类似HashTable都是键/值对
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="theDate" value="2015-07-20 16:25"/> <add key="theName" value="Alice"/> <add key="theType" value="NBA"/> <add key="thePrice" value="12500.00"/> </appSettings> </configuration>
那如何访问App.config配置文件中的参数值呢?
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; namespace AppConfigDemo { class Program { static void Main(string[] args) { //判断App.config配置文件中是否有Key(非null) if (ConfigurationManager.AppSettings.HasKeys()) { //循环遍历出配置文件中的所有的键Key foreach (string s in ConfigurationManager.AppSettings) { Console.WriteLine(s); } } Console.ReadKey(); } } }
使用for循环遍历Key的代码如下:
static void Main(string[] args) { //判断App.config配置文件中是否有Key(非null) if (ConfigurationManager.AppSettings.HasKeys()) { //循环遍历出配置文件中的所有的键Key for (int i = 0; i < ConfigurationManager.AppSettings.Count; i++) { Console.WriteLine(ConfigurationManager.AppSettings.GetKey(i)); } } Console.ReadKey(); }
通过Key访问Value的方法:
static void Main(string[] args) { //判断App.config配置文件中是否有Key(非null) if (ConfigurationManager.AppSettings.HasKeys()) { //获取“theDate”键的Value foreach (string s in ConfigurationManager.AppSettings.GetValues("theDate")) { Console.WriteLine(s); } } Console.ReadKey(); }
如果你想获取所有Key的Value集合,那该怎么办呢?
思路:将所有的Key遍历出后保存在一个容器里(例如:数组),然后用Key匹配找出Value即可。
static void Main(string[] args) { //判断App.config配置文件中是否有Key(非null) if (ConfigurationManager.AppSettings.HasKeys()) { List<string> theKeys = new List<string>(); //保存Key的集合 List<string> theValues = new List<string>(); //保存Value的集合 //遍历出所有的Key并添加进theKeys集合 foreach (string theKey in ConfigurationManager.AppSettings.Keys) { theKeys.Add(theKey); } //根据Key遍历出所有的Value并添加进theValues集合 for (int i = 0; i < theKeys.Count; i++) { foreach (string theValue in ConfigurationManager.AppSettings.GetValues(theKeys[i])) { theValues.Add(theValue); } } //验证一下 Console.WriteLine("*************Key*************"); foreach (string s in theKeys) { Console.WriteLine(s); } Console.WriteLine("************Value************"); foreach (var item in theValues) { Console.WriteLine(item); } } Console.ReadKey(); }
以上代码就是使用.net技术获取app.config配置文件中的参数值的例子,有需要的朋友可以参考下。
相关文章
- 本篇文章给大家分享了C#App.config和Web.config加密的相关知识点以及具体代码步骤,有兴趣的朋友参考学习下。...2020-06-25
- Web.config 和 App.config 的区别分析,需要的朋友可以参考一下...2021-09-22
- 在本篇内容里小编给大家分享了C#获取App.Config配置项的方法和相关知识点,需要的朋友们学习下。...2020-06-25
- C#读写操作app.config中的数据应用介绍;需要的朋友可以参考下...2020-06-25
- 下面小编就为大家带来一篇c# 配置文件App.config操作类库的方法。小编觉的挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- 这篇文章主要介绍了C#中读取App.config配置文件代码实例,包括循环读取、单个读取的方法,需要的朋友可以参考下...2020-06-25
c#读写App.config,ConfigurationManager.AppSettings 不生效的解决方法
这篇文章主要介绍了c#读写App.config,ConfigurationManager.AppSettings 不生效的解决方法,需要的朋友可以参考下...2020-06-25- 这篇文章介绍了获取app.config配置文件中的参数值方法,首先是要添加System.Configuration引用,其次类文件中必须有 using System.Configuration;再次App.config添加,最后向App.config配置文件添加参数,下面通过列子给大家讲解下,需要的朋友可以参考下...2021-09-22
在web.config和app.config文件中增加自定义配置节点的方法
本篇文章主要是对在web.config和app.config文件中增加自定义配置节点的方法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助...2020-06-25VS中C#读取app.config数据库配置字符串的三种方法
这篇文章主要介绍了VS中C#读取app.config数据库配置字符串的三种方法,需要的朋友可以参考下...2020-06-25解决在Web.config或App.config中添加自定义配置的方法详解
本篇文章是对在Web.config或App.config中添加自定义配置的方法进行了详细的分析介绍,需要的朋友参考下...2021-09-22基于动态修改App.Config与web.Config的使用详解
本篇文章是对动态修改App.Config与web.Config的使用进行了详细的分析介绍,需要的朋友参考下...2020-06-25winform中写app.config文件时调试情况下没有改变的原因
读取很简单基本都用过 ConfigurationManager.AppSettings[""].ToString() 写config不是很常用...2020-06-25