web.config中有一些自定义keyvalue配置节,想要在线修改,找了半天没找到现成的,自己写了一个:
就是类似这样的配置节:
<system.framework>
<add key="SystemFramework.Tracing.Enabled" value="True" />
<add key="SystemFramework.Tracing.TraceLevel" value="4" />
</system.framework>
写了4个类:
配置项:NameValueConfigurationItem
NameValueConfigurationItem
public sealed class NameValueConfigurationItem
: System.Configuration.ConfigurationElement
{
private static ConfigurationPropertyCollection _properties
= new ConfigurationPropertyCollection();
private static readonly ConfigurationProperty _value
= new ConfigurationProperty("value",
typeof(string),
string.Empty,
ConfigurationPropertyOptions.IsRequired);
private static readonly ConfigurationProperty _key
= new ConfigurationProperty("key",
typeof(string),
string.Empty,
ConfigurationPropertyOptions.IsKey | ConfigurationPropertyOptions.IsRequired);
static NameValueConfigurationItem()
{
_properties.Add(_key);
_properties.Add(_value);
}
internal NameValueConfigurationItem()
{
}
public NameValueConfigurationItem(string key, string value)
{
base[_key] = key;
base[_value] = value;
}
[ConfigurationProperty("value", IsRequired = true)]
public string Value
{
get
{
return (string)base[_value];
}
}
protected override ConfigurationPropertyCollection Properties
{
get
{
return _properties;
}
}
[ConfigurationProperty("key", IsRequired = true, IsKey = true)]
public string Key
{
get
{
return (string)base[_key];
}
}
}
配置项集合:NameValueConfigurationCollection
CodeNameValueConfigurationCollection
[ConfigurationCollection(typeof(NameValueConfigurationItem))]
public sealed class NameValueConfigurationCollection
: System.Configuration.ConfigurationElementCollection
{
private static readonly ConfigurationPropertyCollection _properties
= new ConfigurationPropertyCollection();
public NameValueConfigurationCollection()
: base(StringComparer.OrdinalIgnoreCase)
{
}
public void Add(NameValueConfigurationItem nameValue)
{
this.BaseAdd(nameValue);
}
public void Clear()
{
base.BaseClear();
}
protected override ConfigurationElement CreateNewElement()
{
return new NameValueConfigurationItem();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((NameValueConfigurationItem)element).Key;
}
public string GetKey(int index)
{
return (string)base.BaseGetKey(index);
}
public void Remove(string name)
{
base.BaseRemove(name);
}
public void Remove(NameValueConfigurationItem nameValue)
{
base.BaseRemove(this.GetElementKey(nameValue));
}
public void RemoveAt(int index)
{
base.BaseRemoveAt(index);
}
public string[] AllKeys
{
get
{
return ObjectArrayToStringArray(base.BaseGetAllKeys());
}
}
static string[] ObjectArrayToStringArray(object[] objectArray)
{
string[] array = new string[objectArray.Length];
objectArray.CopyTo(array, 0);
return array;
}
public NameValueConfigurationItem this[int index]
{
get
{
return (NameValueConfigurationItem)base.BaseGet(index);
}
set
{
if (base.BaseGet(index) != null)
{
base.BaseRemoveAt(index);
}
this.BaseAdd(index, value);
}
}
public new NameValueConfigurationItem this[string name]
{
get
{
return (NameValueConfigurationItem)base.BaseGet(name);
}
}
protected override ConfigurationPropertyCollection Properties
{
get
{
return _properties;
}
}
public bool ContainsKey(string key)
{
if (string.IsNullOrEmpty(key))
return false;
foreach (string temp in AllKeys)
{
if (String.Compare(temp, key, true) == 0)
return true;
}
return false;
}
}
配置节处理类:NameValueConfigurationSection
NameValueConfigurationSection
public class NameValueConfigurationSection
: System.Configuration.ConfigurationSection
{
private static ConfigurationPropertyCollection _properties
= new ConfigurationPropertyCollection();
private static readonly ConfigurationProperty _settings
= new ConfigurationProperty(null,
typeof(NameValueConfigurationCollection),
null,
ConfigurationPropertyOptions.IsDefaultCollection);
static NameValueConfigurationSection()
{
_properties.Add(_settings);
}
/**//// <summary>
///
/// </summary>
[ConfigurationProperty("", IsDefaultCollection = true)]
public NameValueConfigurationCollection Settings
{
get
{
return (NameValueConfigurationCollection)base[_settings];
}
}
}
具体配置节处理类:FrameworkConfiguration
FrameworkConfiguration
public class FrameworkConfiguration
: NameValueConfigurationSection
{
public static FrameworkConfiguration Instance
{
get
{
return System.Web.Configuration.WebConfigurationManager.GetWebApplicationSection("system.framework")
as FrameworkConfiguration;
}
}
}
配置节处理配置:
<configSections>
<section name="system.framework" type="Framework.Configuration.FrameworkConfiguration, Framework" />
</configSections>
修改配置:
System.Configuration.Configuration config
= System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
Mars.Utility.Web.Configuration.NameValueConfigurationSection
section =
(Mars.Utility.Web.Configuration.NameValueConfigurationSection)
config.GetSection("system.framework");
section.Settings.Remove(_key);
section.Settings.Add(new Mars.Utility.Web.Configuration.NameValueConfigurationItem(_key,TextBoxValue.Text));
config.Save(ConfigurationSaveMode.Modified);