获取App.config配置文件中的参数值

所属分类: 网络编程 / ASP.NET 阅读数: 289
收藏 0 赞 0 分享

下面通过代码示例给大家展示下,具体内容如下:

首先添加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配置文件中的参数值的例子,有需要的朋友可以参考下。

更多精彩内容其他人还在看

详解.NET中的加密算法总结(自定义加密Helper类续)

这篇文章主要介绍了详解.NET中的加密算法总结(自定义加密Helper类续) ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享

详谈.NET的异常处理

本文在对异常的介绍中,主要介绍了CLR的异常处理机制,一些较为通用的异常代码,以及对Exception类的介绍。具有很好的参考价值,需要的朋友一起来看下吧
收藏 0 赞 0 分享

浅谈Main方法的参数

本文主要对Main方法的参数通过案例分析进行介绍,具有很好的参考价值,需要的朋友一起来看下吧
收藏 0 赞 0 分享

详解.net循环、逻辑语句块(基础知识)

本篇是介绍.NET 基础部分,主要简述循环,判断,对初学者具有很好的参考借鉴价值,下面就跟小编一起来看下吧
收藏 0 赞 0 分享

BaiduTemplate模板引擎使用示例(附源码)

本文主要分享了BaiduTemplate模板引擎使用示例,具有很好的参考价值,需要的朋友一起来看下吧
收藏 0 赞 0 分享

Asp.net中通过Button打开另一个的frm

本文通过实例代码给大家介绍了asp.net中通过button打开另一个frm的方法,非常不错,需要的朋友参考下吧
收藏 0 赞 0 分享

.NET framework 4.0 安装失败回滚问题

这篇文章主要介绍了.NET framework 4.0 安装失败回滚问题,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

.NET实现文件跨服务器上传下载的方法

这篇文章主要给大家介绍了.NET文件如何实现跨服务器上传下载的方法,文中通过图片介绍的很详细,相信对大家的理解和学习具有一定的参考借鉴价值,有需要的朋友们可以跟着小编来一起学习学习吧。
收藏 0 赞 0 分享

ASP.NET操作MySql数据库的实例代码讲解

这篇文章主要介绍了ASP.NET操作MySql数据库的实例代码讲解,需要的朋友可以参考下
收藏 0 赞 0 分享

Asp.net Core 1.1 升级后操作mysql出错的解决办法

这篇文章主要介绍了Asp.net Core 1.1 升级后操作mysql出错的解决办法,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多