C#新增和修改config配置文件
namespace System.Configuration;
1. 创建配置节类
必须创建继承自ConfigurationSection的对象才能进行配置数据读写操作,ConfigurationSection提供了索引器用来获取和设置配置数据,需要注意的是拥有ConfigurationProperty特性的属性才会被存储,并且名称要保持大小写完全一致,如下面的代码中,所有的"id"必须保持一样。
1. 创建配置节类
必须创建继承自ConfigurationSection的对象才能进行配置数据读写操作,ConfigurationSection提供了索引器用来获取和设置配置数据,需要注意的是拥有ConfigurationProperty特性的属性才会被存储,并且名称要保持大小写完全一致,如下面的代码中,所有的"id"必须保持一样。
class ConfigSectionData : ConfigurationSection
{
[ConfigurationProperty("id")]
public int Id
{
{
[ConfigurationProperty("id")]
public int Id
{
get { return (int)this["id"]; }
set { this["id"] = value; }
}
[ConfigurationProperty("time")]
public DateTime Time
{
get { return (DateTime)this["time"]; }
set { this["time"] = value; }
}
}
set { this["id"] = value; }
}
[ConfigurationProperty("time")]
public DateTime Time
{
get { return (DateTime)this["time"]; }
set { this["time"] = value; }
}
}
2. 创建配置文件操作对象
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserL
evel.None);
ConfigSectionData data = new ConfigSectionData();
data.Id = 1000;
data.Time = DateTime.Now;
config.Sections.Add("add", data);
config.Save(ConfigurationSaveMode.Minimal);
ConfigSectionData data = new ConfigSectionData();
data.Id = 1000;
data.Time = DateTime.Now;
config.Sections.Add("add", data);
config.Save(ConfigurationSaveMode.Minimal);
上面的例子是操作 fig,在根节点(configuration)下写入名称为"add"的配置数据。
需要注意的 VS2005 在IDE模式下会将信息写入 *.fig,并且在程序关闭时覆写该文件,因此您可能看不到您写入的配置数据,只要在资源管理其中执行 *.exe 文件,您就可以在 *.fig 文件中看到结果了。
如果我们需要操作非缺省配置文件,可以使用ExeConfigurationFileMap对象。
ExeConfigurationFileMap file = new ExeConfigurationFileMap();
file.ExeConfigFilename = "fig";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
ConfigSectionData data = new ConfigSectionData();
data.Id = 1000;
data.Time = DateTime.Now;
file.ExeConfigFilename = "fig";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
ConfigSectionData data = new ConfigSectionData();
data.Id = 1000;
data.Time = DateTime.Now;
config.Sections.Add("add", data);
config.Save(ConfigurationSaveMode.Minimal);
config.Save(ConfigurationSaveMode.Minimal);
如果我们不希望在根节点下写入配置数据,可以使用ConfigurationSectionGroup对象。
ExeConfigurationFileMap file = new ExeConfigurationFileMap();
file.ExeConfigFilename = "fig";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
ConfigSectionData data = new ConfigSectionData();
data.Id = 1000;
data.Time = DateTime.Now;
config.SectionGroups.Add("group1", new ConfigurationSectionGroup());
file.ExeConfigFilename = "fig";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
ConfigSectionData data = new ConfigSectionData();
data.Id = 1000;
data.Time = DateTime.Now;
config.SectionGroups.Add("group1", new ConfigurationSectionGroup());
config.SectionGroups["group1"].Sections.Add("add", data);
config.Save(ConfigurationSaveMode.Minimal);
config.Save(ConfigurationSaveMode.Minimal);
下面就是生成的配置文件。
3. 读取配置文件
ExeConfigurationFileMap file = new ExeConfigurationFileMap();
file.ExeConfigFilename = "fig";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
ConfigSectionData data = config.SectionGroups["group1"].Sections["add"] as ConfigSectionData;
//ConfigSectionData data = config.Sections["add"] as ConfigSectionData; // 从根节读取
if (data != null)
{
Console.WriteLine(data.Id);
Console.WriteLine(data.Time);
}
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
ConfigSectionData data = config.SectionGroups["group1"].Sections["add"] as ConfigSectionData;
//ConfigSectionData data = config.Sections["add"] as ConfigSectionData; // 从根节读取
if (data != null)
{
Console.WriteLine(data.Id);
Console.WriteLine(data.Time);
}
4. 写配置文件
在写入 ConfigurationSectionGroup 和 ConfigurationSection 前要判断同名配置是否已经存在,否则会写入失败。
另外如果配置文件被其他Configuration对象修改,则保存会失败,并抛出异常。建议采用Singleton模式。
另外如果配置文件被其他Configuration对象修改,则保存会失败,并抛出异常。建议采用Singleton模式。
ExeConfigurationFileMap file = new ExeConfigurationFileMap();
file.ExeConfigFilename = "fig";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
ConfigSectionData data = new ConfigSectionData();
data.Id = 2000;
data.Time = DateTime.Now;
ConfigurationSectionGroup group1 = config.SectionGroups["group1"];
if (group1 == null)
file.ExeConfigFilename = "fig";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
ConfigSectionData data = new ConfigSectionData();
data.Id = 2000;
data.Time = DateTime.Now;
ConfigurationSectionGroup group1 = config.SectionGroups["group1"];
if (group1 == null)
config.SectionGroups.Add("group1", new ConfigurationSectionGroup());
ConfigurationSection data = group1.Sections["add"] as config;
if (add == null)
config.SectionGroups["group1"].Sections.Add("add", data);
else
{
group1.Sections.Remove("add");
group1.Sections.Add("add", data);
// 或者直接修改原配置对象,前提是类型转换要成功。
//ConfigSectionData configData = add as ConfigSectionData;
//configData.Id = data.Id;
//configData.Time = data.Time;
}
ConfigurationSection data = group1.Sections["add"] as config;
if (add == null)
config.SectionGroups["group1"].Sections.Add("add", data);
else
{
group1.Sections.Remove("add");
group1.Sections.Add("add", data);
// 或者直接修改原配置对象,前提是类型转换要成功。
//ConfigSectionData configData = add as ConfigSectionData;
//configData.Id = data.Id;
//configData.Time = data.Time;
}
config.Save(ConfigurationSaveMode.Minimal);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论