创建配置节类
必须创建继承自ConfigurationSection的对象才能进行配置数据读写操作ConfigurationSection提供了索引器用来获取和设置配置数据需要注意的是拥有ConfigurationProperty特性的属性才会被存储并且名称要保持大小写完全一致如下面的代码中所有的id必须保持一样
class ConfigSectionData : ConfigurationSection
{
[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; }
}
}
创建配置文件操作对象
Configuration config = ConfigurationManagerOpenExeConfiguration(ConfigurationUserLevelNone)
ConfigSectionData data = new ConfigSectionData()
dataId = ;
dataTime = DateTimeNow;
configSectionsAdd(add data)
configSave(ConfigurationSaveModeMinimal)
上面的例子是操作nfig在根节点(configuration)下写入名称为add的配置数据
需要注意的 VS 在IDE模式下会将信息写入*nfig并且在程序关闭时覆写该文件因此您可能看不到您写入的配置数据只要在资源管理其中执行*exe 文件您就可以在*nfig 文件中看到结果了
如果我们需要操作非缺省配置文件可以使用ExeConfigurationFileMap对象
ExeConfigurationFileMap file = new ExeConfigurationFileMap()
fileExeConfigFilename = nfig;
Configuration config = ConfigurationManagerOpenMappedExeConfiguration(file ConfigurationUserLevelNone)
ConfigSectionData data = new ConfigSectionData()
dataId = ;
dataTime = DateTimeNow;
configSectionsAdd(add data)
configSave(ConfigurationSaveModeMinimal)
如果我们不希望在根节点下写入配置数据可以使用ConfigurationSectionGroup对象
ExeConfigurationFileMap file = new ExeConfigurationFileMap()
fileExeConfigFilename = nfig;
Configuration config = ConfigurationManagerOpenMappedExeConfiguration(file
ConfigurationUserLevelNone)
ConfigSectionData data = new ConfigSectionData()
dataId = ;
dataTime = DateTimeNow;
configSectionGroupsAdd(group new ConfigurationSectionGroup())
configSectionGroups[group]SectionsAdd(add data)
configSave(ConfigurationSaveModeMinimal)
下面就是生成的配置文件
读取配置文件
ExeConfigurationFileMap file = new ExeConfigurationFileMap()
fileExeConfigFilename = nfig;
Configuration config = ConfigurationManagerOpenMappedExeConfiguration(file ConfigurationUserLevelNone)
ConfigSectionData data = configSectionGroups[group]Sections[add] as ConfigSectionData;
//ConfigSectionData data = configSections[add] as ConfigSectionData; // 从根节读取
if (data != null)
{
ConsoleWriteLine(dataId)
ConsoleWriteLine(dataTime)
}
写配置文件
在写入 ConfigurationSectionGroup 和 ConfigurationSection 前要判断同名配置是否已经存在否则会写入失败 另外如果配置文件被其他Configuration对象修改则保存会失败并抛出异常建议采用Singleton模式 ExeConfigurationFileMap file = new ExeConfigurationFileMap()
fileExeConfigFilename = nfig;
Configuration config = ConfigurationManagerOpenMappedExeConfiguration(file ConfigurationUserLevelNone)
ConfigSectionData data = new ConfigSectionData()
dataId = ;
dataTime = DateTimeNow;
ConfigurationSectionGroup group = configSectionGroups[group];
if (group == null)
configSectionGroupsAdd(group new ConfigurationSectionGroup())
ConfigurationSection data = groupSections[add] as config;
if (add == null)
configSectionGroups[group]SectionsAdd(add data)
else
{
groupSectionsRemove(add)
groupSectionsAdd(add data)
// 或者直接修改原配置对象前提是类型转换要成功
//ConfigSectionData configData = add as ConfigSectionData;
//configDataId = dataId;
//configDataTime = dataTime;
}
configSave(ConfigurationSaveModeMinimal)
删除配置节
删除ConfigurationSectionGroup configSectionGroupsRemove(group)
//configSectionGroupsClear()
configSave(ConfigurationSaveModeMinimal)
删除ConfigurationSection configSectionsRemove(add)
//configSectionsClear()
if (configSectionGroups[group] != null)
{
configSectionGroups[group]SectionsRemove(add)
//configSectionGroups[group]SectionsClear()
}
configSave(ConfigurationSaveModeMinimal) 其他
可以使用 ConfigurationManagerOpenMachineConfiguration() 来操作 nfig 文件 或者使用 SystemWebConfiguration 名字空间中的 WebConfigurationManager 类来操作 配置文件
ConfigurationManager还提供了AppSettingsConnectionStringsGetSection()等便捷操作 使用自定义类
比如ConfigSectionData里面除了简单类型之外可不可以有自定义的类? 可以使用自定义类不过需要定义一个转换器 using System;
using SystemCollections;
using SystemCollectionsGeneric;
using SystemConfiguration;
using SystemGlobalization;
using SystemComponentModel;
// 要写入配置文件的自定义类
class CustomData
{
public CustomData(string s)
{
thiss = s;
}
private string s;
public string S
{
get { return s; }
set { s = value; }
}
}
// 自定义的转换器(演示代码省略了类型判断)
class CustomConvert : ConfigurationConverterBase
{
public override bool CanConvertFrom(ITypeDescriptorContext ctx Type type)
{
return (type == typeof(string))
}
public override object ConvertTo(ITypeDescriptorContext ctx CultureInfo ci object value Type type)
{
return (value as CustomData)S;
}
public override object ConvertFrom(ITypeDescriptorContext ctx CultureInfo ci object data)
{
return new CustomData((string)data)
}
}
class ConfigSectionData : ConfigurationSection
{
[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; }
}
[ConfigurationProperty(custom)]
[TypeConverter(typeof(CustomConvert))] // 指定转换器
public CustomData Custom
{
get { return (CustomData)this[custom]; }
set { this[custom] = value; }
}
}
public class Program
{
static void Main(string[] args)
{
Configuration config = ConfigurationManagerOpenExeConfiguration (ConfigurationUserLevelNone)
ConfigSectionData data = new ConfigSectionData()
dataId = ;
dataTime = DateTimeNow;
dataCustom = new CustomData(abcdefg…)
configSectionsAdd(add data)
configSave(ConfigurationSaveModeMinimal)
// 读取测试
ConfigSectionData configData = (ConfigSectionData)configSections[add];
ConsoleWriteLine(configDataCustomS)
}
} 保存后的配置文件