如何阅读在C#中的app.config文件中有多个值? [英] How to read multiple values in C# app.config file?

查看:260
本文介绍了如何阅读在C#中的app.config文件中有多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读下面的app.config文件。如何看的呢?我是否需要以读取文件??改变任何东西。

 < XML版本=1.0编码=UTF-8&GT?;
<结构>
<用户>
  <添加用户名=迪内希密码=密码域=MyCompany的/>
 <添加用户名=库玛密码=密码域=MyCompany的/>
< /用户>
< /结构>
 

解决方案

我想你应该实现一个部分。

我做了一些样品code,它可能是你想要什么:

 使用System.Collections.Generic;
使用System.Configuration;
使用System.Linq的;

命名空间ConsoleApplication1
{
    公共密封类UsersConfigMapSection:配置节
    {
        私有静态UsersConfigMapSection配置= ConfigurationManager.GetSection(用户)作为UsersConfigMapSection;

        公共静态UsersConfigMapSection配置
        {
            得到
            {
                返回的配置;
            }
        }

        [的ConfigurationProperty(,IsRequired = TRUE,IsDefaultCollection =真)
        私人UsersConfigMapConfigElements设置
        {
            {返回(UsersConfigMapConfigElements)这个(); }
            集合{这个[] =价值; }
        }

        公开的IEnumerable< UsersConfigMapConfigElement> SettingsList
        {
            {返回this.Settings.Cast< UsersConfigMapConfigElement>(); }
        }
    }

    公共密封类UsersConfigMapConfigElements:ConfigurationElementCollection
    {
        保护覆盖的ConfigurationElement CreateNewElement()
        {
            返回新UsersConfigMapConfigElement();
        }
        保护覆盖对象GetElementKey(的ConfigurationElement元)
        {
            返回((UsersConfigMapConfigElement)元).Username;
        }
    }

    公共密封类UsersConfigMapConfigElement:的ConfigurationElement
    {
        [的ConfigurationProperty(用户名,IsKey = TRUE,IsRequired =真)
        公共字符串用户名
        {
            {返回(串)基地[用户名]; }
            集合{基地[用户名] =值; }
        }

        [的ConfigurationProperty(密码,IsRequired =真)
        公共字符串密码
        {
            {返回(串)基地[密码]; }
            集合{基地[密码] =值; }
        }

        [的ConfigurationProperty(域,IsRequired =真)
        公共字符串域
        {
            {返回(串)基地[域]; }
            集合{基地[域] =值; }
        }
    }
}
 

然后你提取您的配置文件的用户是这样的:

  VAR用户= UsersConfigMapSection.Config.SettingsList.ToList();
 

最后你的配置文件应该是这样的:

 < XML版本=1.0编码=UTF-8&GT?;
<结构>
  < configSections>
    <节名称=用户类型=ConsoleApplication1.UsersConfigMapSection,ConsoleApplication1/>
  < / configSections>
  <用户>
    <添加用户名=迪内希密码=密码域=MyCompany的/>
    <添加用户名=库玛密码=密码域=MyCompany的/>
  < /用户>
    <启动>
        < supportedRuntime版本=4.0版的SKU =NETFramework,版本= V4.5/>
    < /启动>
< /结构>
 

I want to read the following app.config file.. How to read it? Do I need to change anything in order to read the file ??

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<Users>
  <add username = "Dinesh" password ="Password" domain ="MyCompany" />
 <add username = "Kumar" password ="Password" domain ="MyCompany" />
</Users>
</configuration>

解决方案

I think you should implement a section.

I made some sample code that might be exactly what you want:

using System.Collections.Generic;
using System.Configuration;
using System.Linq;

namespace ConsoleApplication1
{
    public sealed class UsersConfigMapSection : ConfigurationSection
    {
        private static UsersConfigMapSection config = ConfigurationManager.GetSection("Users") as UsersConfigMapSection;

        public static UsersConfigMapSection Config
        {
            get
            {
                return config;
            }
        }

        [ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
        private UsersConfigMapConfigElements Settings
        {
            get { return (UsersConfigMapConfigElements)this[""]; }
            set { this[""] = value; }
        }

        public IEnumerable<UsersConfigMapConfigElement> SettingsList
        {
            get { return this.Settings.Cast<UsersConfigMapConfigElement>(); }
        }
    }

    public sealed class UsersConfigMapConfigElements : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new UsersConfigMapConfigElement();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((UsersConfigMapConfigElement)element).Username;
        }
    }

    public sealed class UsersConfigMapConfigElement : ConfigurationElement
    {
        [ConfigurationProperty("username", IsKey = true, IsRequired = true)]
        public string Username
        {
            get { return (string)base["username"]; }
            set { base["username"] = value; }
        }

        [ConfigurationProperty("password", IsRequired = true)]
        public string Password
        {
            get { return (string)base["password"]; }
            set { base["password"] = value; }
        }

        [ConfigurationProperty("domain", IsRequired = true)]
        public string Domain
        {
            get { return (string)base["domain"]; }
            set { base["domain"] = value; }
        }
    }
}

Then you extract users from your config file like this:

var users = UsersConfigMapSection.Config.SettingsList.ToList();

And finally your config file should look like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="Users" type="ConsoleApplication1.UsersConfigMapSection, ConsoleApplication1"/>
  </configSections>
  <Users>
    <add username = "Dinesh" password ="Password" domain ="MyCompany" />
    <add username = "Kumar" password ="Password" domain ="MyCompany" />
  </Users>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

这篇关于如何阅读在C#中的app.config文件中有多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆