添加自定义配置的方法(如果您需要的不仅仅是简单类型)是使用 ConfigurationSection,在您定义的架构中,您需要一个 ConfigurationElementCollection(设置为没有名称的默认集合),其中包含一个 ConfigurationElement,如下所示:
public class UserElement : ConfigurationElement
{
[ConfigurationProperty( "firstName", IsRequired = true )]
public string FirstName
{
get { return (string) base[ "firstName" ]; }
set { base[ "firstName" ] = value;}
}
[ConfigurationProperty( "lastName", IsRequired = true )]
public string LastName
{
get { return (string) base[ "lastName" ]; }
set { base[ "lastName" ] = value; }
}
[ConfigurationProperty( "email", IsRequired = true )]
public string Email
{
get { return (string) base[ "email" ]; }
set { base[ "email" ] = value; }
}
internal string Key
{
get { return string.Format( "{0}|{1}|{2}", FirstName, LastName, Email ); }
}
}
[ConfigurationCollection( typeof(UserElement), AddItemName = "user", CollectionType = ConfigurationElementCollectionType.BasicMap )]
public class UserElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new UserElement();
}
protected override object GetElementKey( ConfigurationElement element )
{
return ( (UserElement) element ).Key;
}
public void Add( UserElement element )
{
BaseAdd( element );
}
public void Clear()
{
BaseClear();
}
public int IndexOf( UserElement element )
{
return BaseIndexOf( element );
}
public void Remove( UserElement element )
{
if( BaseIndexOf( element ) >= 0 )
{
BaseRemove( element.Key );
}
}
public void RemoveAt( int index )
{
BaseRemoveAt( index );
}
public UserElement this[ int index ]
{
get { return (UserElement) BaseGet( index ); }
set
{
if( BaseGet( index ) != null )
{
BaseRemoveAt( index );
}
BaseAdd( index, value );
}
}
}
public class UserInfoSection : ConfigurationSection
{
private static readonly ConfigurationProperty _propUserInfo = new ConfigurationProperty(
null,
typeof(UserElementCollection),
null,
ConfigurationPropertyOptions.IsDefaultCollection
);
private static ConfigurationPropertyCollection _properties = new ConfigurationPropertyCollection();
static UserInfoSection()
{
_properties.Add( _propUserInfo );
}
[ConfigurationProperty( "", Options = ConfigurationPropertyOptions.IsDefaultCollection )]
public UserElementCollection Users
{
get { return (UserElementCollection) base[ _propUserInfo ]; }
}
}
我使 UserElement 类保持简单,尽管它确实应该遵循完全声明每个属性的模式,如这篇出色的 CodeProject 文章中所述。如您所见,它代表您提供的配置中的“用户”元素。
UserElementCollection 类只支持拥有多个“用户”元素,包括在运行时修改集合中添加/删除/清除项目的能力。
最后是 UserInfoSection,它简单地统计它有一个默认的“用户”元素集合。
接下来是 App.config 文件的示例:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup>
<section
name="userInfo"
type="ConsoleApplication1.UserInfoSection, ConsoleApplication1"
allowDefinition="Everywhere"
allowExeDefinition="MachineToLocalUser"
/>
</sectionGroup>
</configSections>
<userInfo>
<user firstName="John" lastName="Doe" email="John.Doe@company.com" />
<user firstName="Jane" lastName="Doe" email="Jane.Doe@company.com" />
</userInfo>
</configuration>
如您所见,在此示例中,我在 App.config 中包含了一些 userInfo/user 元素。我还添加了设置,表示可以在机器/应用程序/用户/漫游用户级别定义它们。
接下来我们需要知道如何在运行时更新它们,下面的代码展示了一个例子:
Configuration userConfig = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.PerUserRoamingAndLocal );
var userInfoSection = userConfig.GetSection( "userInfo" ) as UserInfoSection;
var userElement = new UserElement();
userElement.FirstName = "Sample";
userElement.LastName = "User";
userElement.Email = "Sample.User@company.com";
userInfoSection.Users.Add( userElement );
userConfig.Save();
如果需要,上面的代码将创建一个新的 user.config 文件,该文件深埋在用户的“Local Settings\Application Data”文件夹中。
如果您希望将新用户添加到 app.config 文件中,只需将 OpenExeConfiguration() 方法的参数更改为 ConfigurationUserLevel.None。
正如你所看到的,它相当简单,尽管找到这些信息需要一些挖掘。