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

所属分类: 网络编程 / ASP.NET 阅读数: 320
收藏 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配置文件中的参数值的例子,有需要的朋友可以参考下。

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

asp.net中利用ashx实现图片防盗链代码

直接分析盗链原理:看下面用httpwatch截获的http发送的数据
收藏 0 赞 0 分享

ASP.NET程序中常用代码汇总

对于学习asp.net的朋友能用的到
收藏 0 赞 0 分享

asp.net 无重复随机数代码

asp.net产生无重复随机数的实现代码
收藏 0 赞 0 分享

asp.net(C#)中上传大文件的几中常见应用方法

最近博客需要做一个文件上下载功能,我从网上找了点资料,整理了下希望对大家有帮助!
收藏 0 赞 0 分享

asp.net AJAX实现无刷新获得数据

提供一个使用AJAX实现无刷新判断注册用户名是否被注册的代码:
收藏 0 赞 0 分享

Ajax.net Sys未定义错误解决办法

用Asp.net2.0开发的系统,使用了Ajax技术,在本地没有任何问题!但是发布到Web托管服务器上后,系统总是出现“Sys 未定义”的错误!
收藏 0 赞 0 分享

asp.net(c#)判断远程图片是否存在

不错的应用,大家可以拓展到,判断远程文件是否存在等功能
收藏 0 赞 0 分享

asp.net保存远程图片的代码

最近有点烦,没怎么看书,几天下来,就研究了一个保存远程图片的。
收藏 0 赞 0 分享

Asp.Net类库中发送电子邮件的代码

发送电子邮件是许多需要用户注册的网站的通用功能,通过正则表达式我们可以过滤掉不符合电子邮件格式的输入,但是仍没有办法确保用户填写的电子邮件地址一定是他本人真实有效的电子邮件地址
收藏 0 赞 0 分享

ASP.NET表单验证方法详解第1/2页

在表单提交的时候,经常需要对录入信息的长度、格式、内容等进行验证,以便获得合理的信息。在ASP.NET开发中主要的验证方法,我总结了一下,主要有一下几种,如有不足之处请朋友们予以指出。
收藏 0 赞 0 分享
查看更多