【问题标题】:How to read multiple values in C# app.config file?如何在 C# app.config 文件中读取多个值?
【发布时间】:2014-03-19 15:48:35
【问题描述】:
我想阅读下面的 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>
【问题讨论】:
标签:
c#
.net
visual-studio-2008
configuration
app-config
【解决方案1】:
我认为你应该实现一个部分。
我制作了一些可能正是您想要的示例代码:
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>
【解决方案2】:
或者您可以使用此解决方法来实现相同的...
<add key="username" value="A,B,C"/>
And
string[] mykey = ConfigurationManager.AppSettings["username"].Split(',');
【解决方案3】:
首先您需要将您的值放在<appSettings> 下,然后像这样将密钥添加到您想要的字段
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="username" value="Dinesh" />
<appSettings>
</configuration>
然后您需要在您的引用文件夹中添加对System.Configuration 的引用。
现在阅读您的价值...
String Version = ConfigurationManager.AppSettings["username"];
【讨论】:
-
-
-
否,如果您想从 web.config 获取另一个用户名,您需要添加另一个具有不同键的 xml 标签,例如 和 因为在代码中你将通过键而不是值来访问。