南强小屋 Design By 杰米
下面通过代码示例给大家展示下,具体内容如下:
首先添加System.Configuration引用
向App.config配置文件添加参数
App.config添加
向App.config配置文件添加参数
例子:
在这个App.config配置文件中,我添加了4个参数,App.config参数类似HashTable都是键/值对
<"1.0" encoding="utf-8" "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配置文件中的参数值的例子,有需要的朋友可以参考下。
南强小屋 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
南强小屋 Design By 杰米
暂无获取App.config配置文件中的参数值的评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
