




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C#新增和修改config配置文件namespace System.Configuration;1. 创建配置节类必须创建继承自ConfigurationSection的对象才能进行配置数据读写操作,ConfigurationSection提供了索引器用来获取和设置配置数据,需要注意的是拥有ConfigurationProperty特性的属性才会被存储,并且名称要保持大小写完全一致,如下面的代码中,所有的id必须保持一样。class ConfigSectionData : ConfigurationSectionConfigurationProperty(id)public int Idget return (int)thisid; set thisid = value; ConfigurationProperty(time)public DateTime Timeget return (DateTime)thistime; set thistime = value; 2. 创建配置文件操作对象Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);ConfigSectionData data = new ConfigSectionData();data.Id = 1000;data.Time = DateTime.Now;config.Sections.Add(add, data);config.Save(ConfigurationSaveMode.Minimal);上面的例子是操作 app.config,在根节点(configuration)下写入名称为add的配置数据。需要注意的 VS2005 在IDE模式下会将信息写入 *.vshost.exe.config,并且在程序关闭时覆写该文件,因此您可能看不到您写入的配置数据,只要在资源管理其中执行 *.exe 文件,您就可以在 *.exe.config 文件中看到结果了。如果我们需要操作非缺省配置文件,可以使用ExeConfigurationFileMap对象。ExeConfigurationFileMap file = new ExeConfigurationFileMap();file.ExeConfigFilename = test.config;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);如果我们不希望在根节点下写入配置数据,可以使用ConfigurationSectionGroup对象。ExeConfigurationFileMap file = new ExeConfigurationFileMap();file.ExeConfigFilename = test.config;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.SectionGroupsgroup1.Sections.Add(add, data);config.Save(ConfigurationSaveMode.Minimal);下面就是生成的配置文件。3. 读取配置文件ExeConfigurationFileMap file = new ExeConfigurationFileMap();file.ExeConfigFilename = test.config;Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);ConfigSectionData data = config.SectionGroupsgroup1.Sectionsadd as ConfigSectionData;/ConfigSectionData data = config.Sectionsadd as ConfigSectionData; / 从根节读取if (data != null)Console.WriteLine(data.Id);Console.WriteLine(data.Time);4. 写配置文件在写入 ConfigurationSectionGroup 和 ConfigurationSection 前要判断同名配置是否已经存在,否则会写入失败。另外如果配置文件被其他Configuration对象修改,则保存会失败,并抛出异常。建议采用Singleton模式。ExeConfigurationFileMap file = new ExeConfigurationFileMap();file.ExeConfigFilename = test.config;Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);ConfigSectionData data = new ConfigSectionData();data.Id = 2000;data.Time = DateTime.Now;ConfigurationSectionGroup group1 = config.SectionGroupsgroup1;if (group1 = null) config.SectionGroups.Add(group1, new ConfigurationSectionGroup();ConfigurationSection data = group1.Sectionsadd as config;if (add = null)config.SectionGroupsgroup1.Sections.Add(add, data);elsegroup1.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);5. 删除配置节删除ConfigurationSectionGroup config.SectionGroups.Remove(group1);/config.SectionGroups.Clear();config.Save(ConfigurationSaveMode.Minimal);删除ConfigurationSection config.Sections.Remove(add1);/config.Sections.Clear();if (config.SectionGroupsgroup1 != null)config.SectionGroupsgroup1.Sections.Remove(add2);/config.SectionGroupsgroup1.Sections.Clear();config.Save(ConfigurationSaveMode.Minimal);6. 其他可以使用 ConfigurationManager.OpenMachineConfiguration() 来操作 Machine.config 文件。或者使用 System.Web.Configuration 名字空间中的 WebConfigurationManager 类来操作 ASP.net 配置文件。ConfigurationManager还提供了AppSettings、ConnectionStrings、GetSection()等便捷操作。7. 使用自定义类 2006-4-17 补充,回复 ggy 网友的疑问。 引用至 ggy比如ConfigSectionData里面除了简单类型之外,可不可以有自定义的类可以使用自定义类,不过需要定义一个转换器。using System;using System.Collections;using System.Collections.Generic;using System.Configuration;using System.Globalization;using System.ComponentModel;/ 要写入配置文件的自定义类class CustomDatapublic CustomData(string s)this.s = s;private string s;public string Sget return s; set s = value; / 自定义的转换器(演示代码省略了类型判断)class CustomConvert : ConfigurationConverterBasepublic 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 : ConfigurationSectionConfigurationProperty(id)public int Idget return (int)thisid; set thisid = value; ConfigurationProperty(time)public DateTime Timeget return (DateTime)thistime; set thistime = value; ConfigurationProperty(custom)TypeConverter(typeof(CustomConvert) / 指定转换器public CustomData Customget return (CustomData)thiscustom; set thiscustom = value; public class Programstatic void Main(string args)Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);ConfigSectionData data = new ConfigSectionData();data.Id = 1000;data.Time = DateTime.Now;data.Custom = new CustomData
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 历史专业考研试题及答案
- 审计专业原理试题及答案
- 湖南省湖湘名校联盟2025-2026学年高二上学期入学考试语文试卷含答案
- 保卫消防专业试题及答案
- JavaEE轻量级框架Struts2 spring Hibernate整合开发 第4章Struts2高级特性
- 大学专业试题及答案
- 美容店策划活动方案
- 抗疫歌唱活动策划方案
- 家庭聚会致辞材料
- 时尚潮流发布活动指引法
- 2025年海关关务测试题及答案
- (正式版)DB3302∕T 1180-2025 《高速公路建设韧性指标体系》
- 2025年FSC认证原材料采购合同范本
- 2025年8月广东深圳市光明区住房和建设局招聘一般专干5人备考练习题库及答案解析
- 《煤矿安全规程(2025)》防治水新旧条文对照
- GB 16807-2025防火膨胀密封件
- 麻醉医生进修汇报课件
- 开学第一课+课件-2025-2026学年人教版(2024)七年级英语上册
- 医院医疗收费培训课件
- 大咯血的急救和护理
- 名学快问快答题目及答案
评论
0/150
提交评论