一,按照xml文件处理:
配置文件如下图(最后的图片).
自动写入configSections和configSections的实例
1.自动写入configSections
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); LasteventSettingSection last = new LasteventSettingSection(); config.Sections.Add("lastevent", last);
config.Save();
2.自动写入实例
我觉得不应该将.config文件当成xml来操作.但是一直没有找到方法用ConfigurationManager来实现,先用这个顶着.
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load("ConfigurationTest.exe.Config"); XmlNodeList nodes = doc.ChildNodes[].ChildNodes; foreach (XmlNode node in nodes)
{
Console.WriteLine(node.InnerXml);
} XmlNode newnode = doc.ChildNodes[]; foreach (XmlNode v in newnode.ChildNodes)
{
if (v.Name == "lastevent")
{
Console.WriteLine("lastevent 已经存在");
return;
}
} XmlElement elem = doc.CreateElement("lastevent");
XmlAttribute att = doc.CreateAttribute("name");
att.Value = "用于替换lastevent中不想看到的内容";
elem.Attributes.Append(att); XmlElement Items = doc.CreateElement("Items");
elem.AppendChild(Items); XmlElement add1 = doc.CreateElement("add"); XmlAttribute original = doc.CreateAttribute("original");
original.Value = "original";
add1.Attributes.Append(original); XmlAttribute replacement = doc.CreateAttribute("replacement");
replacement.Value = "replacement";
add1.Attributes.Append(replacement); Items.AppendChild(add1); elem.AppendChild(Items); newnode.AppendChild(elem); doc.Save("111.config");
二.用ConfigurationManager类来处理
上面提到的不用xml处理的方法,已经找到了.
原来配置文件为
运行代码后变成:
代码为:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); LasteventSettingSection section = new LasteventSettingSection();
section.Name = "替换"; ConfigurationTest.Items its = new ConfigurationTest.Items(); Item it = new Item();
it.Original = "error";
it.Replacement = "information"; its.Add(it); it = new Item();
it.Original = "error2";
it.Replacement = "information2"; its.Add(it); section.Items = its; LasteventSettingSection lastevent = (LasteventSettingSection)config.Sections["lastevent"];
if (lastevent == null) {
config.Sections.Add("lastevent", section);
}
config.Save();
实现的类如下:
注意:前面几个例子中,继承ConfigurationElementCollection的Items没有实现add,remove方法,必须实现才可以.
class LasteventSettingSection : System.Configuration.ConfigurationSection
{
[ConfigurationProperty("name", IsRequired = false)]
public string Name
{
get { return (string)base["name"]; }
set { base["name"] = value; }
} [ConfigurationProperty("items", IsDefaultCollection = false)]
[ConfigurationCollection(typeof(Item), CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap, RemoveItemName = "remove")]
public Items Items
{
get { return (Items)base["items"]; }
set { base["items"] = value; }
}
} class Item : ConfigurationElement
{
[ConfigurationProperty("original", IsRequired = true, IsKey = true)]
public string Original
{
get { return (string)base["original"]; }
set { base["original"] = value; }
}
[ConfigurationProperty("replacement", IsRequired = true)]
public string Replacement
{
get { return (string)base["replacement"]; }
set { base["replacement"] = value; }
}
} class Items : ConfigurationElementCollection
{
protected override object GetElementKey(ConfigurationElement element)
{
return ((Item)element).Original;
} protected override ConfigurationElement CreateNewElement()
{
return new Item();
} public Item this[int i]
{
get { return (Item)base.BaseGet(i); }
} new public Item this[string key]
{
get { return (Item)base.BaseGet(key); }
} public int IndexOf(Item url)
{
return BaseIndexOf(url);
} public void Add(Item url)
{
BaseAdd(url); // Your custom code goes here. } protected override void BaseAdd(ConfigurationElement element)
{
BaseAdd(element, false); // Your custom code goes here. } public void Remove(Item url)
{
if (BaseIndexOf(url) >= )
{
BaseRemove(url.Original);
// Your custom code goes here.
Console.WriteLine("UrlsCollection: {0}", "Removed collection element!");
}
} public void RemoveAt(int index)
{
BaseRemoveAt(index); // Your custom code goes here. } public void Remove(string name)
{
BaseRemove(name); // Your custom code goes here. } public void Clear()
{
BaseClear(); // Your custom code goes here.
Console.WriteLine("UrlsCollection: {0}", "Removed entire collection!");
} }