读取配置文件
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()等便捷操作
使用自定义类
补充回复 ggy 网友的疑问
引用至 ggy
比如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);
}
}
保存后的配置文件
更详细的信息可以看 MSDN 中关于 SystemConfigurationConfigurationConverterBase 的说明