如何在C# app.config文件中读取多个值?

6

我想读取以下的app.config文件。如何读取它?我需要更改任何内容才能读取该文件吗?

<?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>
3个回答

12

我认为你应该实现一个部分。

我编写了一些示例代码,可能正是你想要的:

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; }
        }
    }
}

然后你可以像这样从配置文件中提取用户:

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

最后,您的配置文件应该像这样:

<?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>

1
您可以使用这个解决方案来达到相同的效果...
<add key="username" value="A,B,C"/>
And

string[] mykey = ConfigurationManager.AppSettings["username"].Split(',');

0
首先,您需要将您的值放在<appSettings>下面,然后像这样为您想要的字段添加键。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
  <add key="username" value="Dinesh" />
<appSettings>
</configuration>

然后您需要在引用文件夹中添加对System.Configuration的引用。

现在读取您的值...

String Version = ConfigurationManager.AppSettings["username"];

如果有两个用户名,我该怎么办? - Dineshkumar
然后你需要为第二个用户名添加另一个键。 - Aftab Ahmed
如果你想从web.config获取另一个用户名,你需要添加另一个XML标签,使用不同的键,例如<add key="username" value="Dinesh" />和<add key="username1" value="Kumar" />。因为在代码中,你将通过键来访问而不是值。 - Aftab Ahmed

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接