今天看到一个问题,关于修改config文件的,我第一个想法是通过XML进行修改,毕竟config也是一个XML文件
class FavoriteUrls
{
public string key { get; set; }
public string value { get; set; }
}
public void InitList()
{
FavoriteUrls item = new FavoriteUrls();
item.key = "Microsoft";
item.value = "http://www.microsoft.com";
list.Add(item);
item = new FavoriteUrls();
item.key = "DotNetSpider";
item.value = "http://www.DotNetSpider.com";
list.Add(item);
item = new FavoriteUrls();
item.key = "AsianSpider";
item.value = "http://www.AsianSpider.com";
list.Add(item);
}
public void RWAppConfig() //在配置文件中增加节点
{
XmlDocument doc = new XmlDocument();
doc.Load("..\\..\\App.config");
XmlNode root = doc.DocumentElement;
XmlElement elem = doc.CreateElement("FavoriteUrls");
for (int i = 0; i < list.Count; i++)
{
XmlElement elemAdd = doc.CreateElement("Add");
XmlAttribute attributeValue = doc.CreateAttribute("value");
XmlAttribute attributeKey = doc.CreateAttribute("key");
attributeKey.InnerText = list[i].key;
attributeValue.InnerText = list[i].value;
elemAdd.Attributes.Append(attributeKey);
elemAdd.Attributes.Append(attributeValue);
elem.AppendChild(elemAdd);
}
root.AppendChild(elem);
doc.Save("..\\..\\App.config");
}
如上面代码所示,通过XML的读取来在app.config中增加了三个节点,效果如下
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<FavoriteUrls>
<Add key="Microsoft" value="http://www.microsoft.com" />
<Add key="DotNetSpider" value="http://www.DotNetSpider.com" />
<Add key="AsianSpider" value="http://www.AsianSpider.com" />
</FavoriteUrls>
</configuration>
现在需要Configuration进行修改config达到同样的目的。
public void AddAppConfig()
{
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); //打开配置文件
config.SectionGroups.Add("FavoriteUrls", new ConfigurationSectionGroup()); //在config声明SectionGroups——"FavoriteUrls"
ConfigurationSectionGroup controlItem = config.SectionGroups["FavoriteUrls"]; //在config中增加SectionGroups
int i = 0;
foreach (FavoriteUrls item in list)
{
i++;
FavoriteUrlsSection section = new FavoriteUrlsSection(); //声明一个节点
section.Key = item.key;
section.Value = item.value;
string sectionName = "add" +i;//通过i的变化来增加add1,add2,add3
controlItem.Sections.Add(sectionName, section); //把节点添加到SectionGroups
}
config.SaveAs("..\\..\\App.config",ConfigurationSaveMode.Full);
}
其执行效果如下 :
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="FavoriteUrls" type="System.Configuration.ConfigurationSectionGroup, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" >
<section name="add1" type="EpsonPrinter.FavoriteUrlsSection, EpsonPrinter, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowLocation="true" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" overrideModeDefault="Allow" restartOnExternalChanges="true" requirePermission="true" />
<section name="add2" type="EpsonPrinter.FavoriteUrlsSection, EpsonPrinter, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowLocation="true" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" overrideModeDefault="Allow" restartOnExternalChanges="true" requirePermission="true" />
<section name="add3" type="EpsonPrinter.FavoriteUrlsSection, EpsonPrinter, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowLocation="true" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" overrideModeDefault="Allow" restartOnExternalChanges="true" requirePermission="true" />
</sectionGroup>
</configSections>
<FavoriteUrls>
<add1 Key="Microsoft" value="http://www.microsoft.com" />
<add2 Key="DotNetSpider" value="http://www.DotNetSpider.com" />
<add3 Key="AsianSpider" value="http://www.AsianSpider.com" />
</FavoriteUrls>
</configuration>
大概一看是有些相似,但是通过Configuration来修改的config文件中
<FavoriteUrls>
<add1 Key="Microsoft" value="http://www.microsoft.com" />
<add2 Key="DotNetSpider" value="http://www.DotNetSpider.com" />
<add3 Key="AsianSpider" value="http://www.AsianSpider.com" />
</FavoriteUrls>
add1,add2,和add3,如果相同的情况下,则会报错:配置文件中已经有相同的节点
如有谁知道还请相告,毕竟这样的情况下不如XML这样操作来的直接。。。
另外,修改好了,就要读取了,
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSectionGroup controlItem = config.SectionGroups["FavoriteUrls"];
FavoriteUrlsSection favorite= controlItem.Sections["add1"] as FavoriteUrlsSection;