.net Framework 2.0 专门提供了配置文件的操作。
namespace System.Configuration;
1. 创建配置节类
必须创建继承自ConfigurationSection的对象才能进行配置数据读写操作,ConfigurationSection提供了索引器用来获取和设置配置数据,需要注意的是拥有ConfigurationProperty特性的属性才会被存储,并且名称要保持大小写完全一致,如下面的代码中,所有的"id"必须保持一样。
class ConfigSectionData : ConfigurationSection
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
[ConfigurationProperty("id")]
public int Id
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return (int)this["id"]; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ this["id"] = value; }
}
![](/Images/OutliningIndicators/InBlock.gif)
[ConfigurationProperty("time")]
public DateTime Time
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return (DateTime)this["time"]; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ this["time"] = value; }
}
}
![](/Images/OutliningIndicators/None.gif)
2. 创建配置文件操作对象
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
![](/Images/OutliningIndicators/None.gif)
ConfigSectionData data = new ConfigSectionData();
data.Id = 1000;
data.Time = DateTime.Now;
![](/Images/OutliningIndicators/None.gif)
config.Sections.Add("add", data);
config.Save(ConfigurationSaveMode.Minimal);
![](/Images/OutliningIndicators/None.gif)
上面的例子是操作 app.config,在根节点(configuration)下写入名称为"add"的配置数据。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="add" type="ConsoleApplication1.ConfigSectionData,
/>
</configSections>
<add id="1000" time="02/18/2006 21:51:06" />
</configuration>
![](/Images/OutliningIndicators/None.gif)
需要注意的 VS2005 在IDE模式下会将信息写入 *.vshost.exe.config,并且在程序关闭时覆写该文件,因此您可能看不到您写入的配置数据,只要在资源管理其中执行 *.exe 文件,您就可以在 *.exe.config 文件中看到结果了。
如果我们需要操作非缺省配置文件,可以使用ExeConfigurationFileMap对象。
ExeConfigurationFileMap file = new ExeConfigurationFileMap();
file.ExeConfigFilename = "test.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
![](/Images/OutliningIndicators/None.gif)
ConfigSectionData data = new ConfigSectionData();
data.Id = 1000;
data.Time = DateTime.Now;
![](/Images/OutliningIndicators/None.gif)
config.Sections.Add("add", data);
config.Save(ConfigurationSaveMode.Minimal);
![](/Images/OutliningIndicators/None.gif)
如果我们不希望在根节点下写入配置数据,可以使用ConfigurationSectionGroup对象。
ExeConfigurationFileMap file = new ExeConfigurationFileMap();
file.ExeConfigFilename = "test.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
![](/Images/OutliningIndicators/None.gif)
ConfigSectionData data = new ConfigSectionData();
data.Id = 1000;
data.Time = DateTime.Now;
![](/Images/OutliningIndicators/None.gif)
config.SectionGroups.Add("group1", new ConfigurationSectionGroup());
config.SectionGroups["group1"].Sections.Add("add", data);
![](/Images/OutliningIndicators/None.gif)
config.Save(ConfigurationSaveMode.Minimal);
![](/Images/OutliningIndicators/None.gif)
下面就是生成的配置文件。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="group1" type="System.Configuration.ConfigurationSectionGroup,
>
<section name="add" type="ConsoleApplication1.ConfigSectionData,
/>
</sectionGroup>
</configSections>
<group1>
<add id="1000" time="02/18/2006 22:01:02" />
</group1>
</configuration>
![](/Images/OutliningIndicators/None.gif)
3. 读取配置文件
ExeConfigurationFileMap file = new ExeConfigurationFileMap();
file.ExeConfigFilename = "test.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
![](/Images/OutliningIndicators/None.gif)
ConfigSectionData data = config.SectionGroups["group1"].Sections["add"] as ConfigSectionData;
//ConfigSectionData data = config.Sections["add"] as ConfigSectionData; // 从根节读取
![](/Images/OutliningIndicators/None.gif)
if (data != null)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
Console.WriteLine(data.Id);
Console.WriteLine(data.Time);
}
![](/Images/OutliningIndicators/None.gif)
4. 写配置文件
在写入 ConfigurationSectionGroup 和 ConfigurationSection 前要判断同名配置是否已经存在,否则会写入失败。
另外如果配置文件被其他Configuration对象修改,则保存会失败,并抛出异常。建议采用Singleton模式。
ExeConfigurationFileMap file = new ExeConfigurationFileMap();
file.ExeConfigFilename = "test.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
![](/Images/OutliningIndicators/None.gif)
ConfigSectionData data = new ConfigSectionData();
data.Id = 2000;
data.Time = DateTime.Now;
![](/Images/OutliningIndicators/None.gif)
ConfigurationSectionGroup group1 = config.SectionGroups["group1"];
if (group1 == null)
config.SectionGroups.Add("group1", new ConfigurationSectionGroup());
![](/Images/OutliningIndicators/None.gif)
ConfigurationSection data = group1.Sections["add"] as config;
if (add == null)
config.SectionGroups["group1"].Sections.Add("add", data);
else
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
group1.Sections.Remove("add");
group1.Sections.Add("add", data);
![](/Images/OutliningIndicators/InBlock.gif)
// 或者直接修改原配置对象,前提是类型转换要成功。
//ConfigSectionData configData = add as ConfigSectionData;
//configData.Id = data.Id;
//configData.Time = data.Time;
}
![](/Images/OutliningIndicators/None.gif)
config.Save(ConfigurationSaveMode.Minimal);
![](/Images/OutliningIndicators/None.gif)
5. 删除配置节
删除ConfigurationSectionGroup
config.SectionGroups.Remove("group1");
//config.SectionGroups.Clear();
![](/Images/OutliningIndicators/None.gif)
config.Save(ConfigurationSaveMode.Minimal);
![](/Images/OutliningIndicators/None.gif)
删除ConfigurationSection
config.Sections.Remove("add1");
//config.Sections.Clear();
![](/Images/OutliningIndicators/None.gif)
if (config.SectionGroups["group1"] != null)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
config.SectionGroups["group1"].Sections.Remove("add2");
//config.SectionGroups["group1"].Sections.Clear();
}
![](/Images/OutliningIndicators/None.gif)
config.Save(ConfigurationSaveMode.Minimal);
![](/Images/OutliningIndicators/None.gif)
6. 其他
可以使用 ConfigurationManager.OpenMachineConfiguration() 来操作 Machine.config 文件。
或者使用 System.Web.Configuration 名字空间中的 WebConfigurationManager 类来操作 ASP.net 配置文件。
ConfigurationManager还提供了AppSettings、ConnectionStrings、GetSection()等便捷操作。