最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

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

来源:动视网 责编:小采 时间:2020-11-27 22:38:24
文档

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

获取App.config配置文件中的参数值:下面通过代码示例给大家展示下,具体内容如下: 首先添加System.Configuration引用 向App.config配置文件添加参数 App.config添加 向App.config配置文件添加参数 例子: 在这个App.config配置文件中,我添加了4个参数,App.config参数类似Has
推荐度:
导读获取App.config配置文件中的参数值:下面通过代码示例给大家展示下,具体内容如下: 首先添加System.Configuration引用 向App.config配置文件添加参数 App.config添加 向App.config配置文件添加参数 例子: 在这个App.config配置文件中,我添加了4个参数,App.config参数类似Has

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

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

文档

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

获取App.config配置文件中的参数值:下面通过代码示例给大家展示下,具体内容如下: 首先添加System.Configuration引用 向App.config配置文件添加参数 App.config添加 向App.config配置文件添加参数 例子: 在这个App.config配置文件中,我添加了4个参数,App.config参数类似Has
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top