1.新建配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="Components" type="Mi.CompoentConfigurationSection,Mi"/>
</configSections>
<Components BasePath="~\Libs\Pool\">
<add DBComponentID="1" DatabaseName="master" SortNo="1" />
<add DBComponentID="2" DatabaseName="Student" SortNo="2" />
</Components>
</configuration>
2.新建组件配置项类
/// <summary>
/// 组件配置项
/// </summary>
[Serializable]
public class ComponentElement : ConfigurationElement
{
[ConfigurationProperty("DBComponentID", IsRequired = true)]
public int DBComponentID
{
get { return Convert.ToInt32(this["DBComponentID"]); }
set { this["DBComponentID"] = value; }
}
[ConfigurationProperty("DatabaseName", IsRequired = true)]
public string DatabaseName
{
get { return this["DatabaseName"].ToString(); }
set { this["DatabaseName"] = value; }
}
[ConfigurationProperty("SortNo", IsRequired = false, DefaultValue = 0)]
public int SortNo
{
get { return Convert.ToInt32(this["SortNo"]); }
set { this["SortNo"] = value; }
}
}
3.新建组件配置集合类
/// <summary>
/// 组件配置集合
/// </summary>
[ConfigurationCollection(typeof(ComponentElement))]
public class ComponentCollection : ConfigurationElementCollection
{
public ComponentCollection() : base(StringComparer.OrdinalIgnoreCase)
{
}
protected override ConfigurationElement CreateNewElement()
{
return new ComponentElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return (element as ComponentElement).DBComponentName;
}
new public ComponentElement this[string dbComponentName]
{
get
{
return (ComponentElement)base.BaseGet(dbComponentName);
}
}
public void Add(ComponentElement item)
{
this.BaseAdd(item);
}
public void Clear()
{
base.BaseClear();
}
public void Remove(string databaseName)
{
base.BaseRemove(databaseName);
}
}
4.新建组件配置节点类
/// <summary>
/// 组件配置节点
/// </summary>
public class CompoentConfigurationSection : ConfigurationSection
{
private static readonly ConfigurationProperty s_property = new ConfigurationProperty(string.Empty, typeof(ComponentCollection), null, ConfigurationPropertyOptions.IsDefaultCollection);
[ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
public ComponentCollection Components
{
get
{
return (ComponentCollection)base[s_property];
}
}
[ConfigurationProperty("BasePath", IsRequired = false)]
public string BasePath
{
get
{
return ReMapBasePath(this["BasePath"].ToString());
}
set
{
this["BasePath"] = ReMapBasePath(value);
}
}
private string ReMapBasePath(string basePath)
{
if (basePath.Trim().StartsWith("~\\"))
{
basePath = basePath.Replace("~\\", AppDomain.CurrentDomain.BaseDirectory + "\\");
}
return basePath;
}
}
5.使用方式
/// <summary>
/// 只读组件配置项
/// </summary>
public static readonly CompoentConfigurationSection ReadOnlyCompoents = ConfigurationManager.GetSection("Components") as CompoentConfigurationSection;
public static readonly Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
/// <summary>
/// 读写主件配置项
/// </summary>
public static CompoentConfigurationSection ReadWriteCompoents =
configuration.GetSection("Components") as CompoentConfigurationSection;
ReadWriteCompoents .Components.Remove("master");//删除
//读写
foreach (ComponentElement itemComponent in ReadWriteCompoents .Components)
{
itemComponent.DatabaseName="MyStudent";
}
configuration.Save(ConfigurationSaveMode.Modified);//只保存修改变动