




已阅读5页,还剩10页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C#中配置文件的操作注:从网上收集整理了关于C#中配置文件的资料,以下是自己的学习笔记,希望能给大家一点帮助,如有错漏,恳请指出。1 配置文件介绍程序配置文件实质上是应用程序的一种针对配置信息读写最简单的工具。它重要的功能就是将命令行选项和配置文件选项统一到一种数据结构中,这样的话可以在外部设置参数,而无须修改代码来达到我们想要的效果。配合上配置文件的读写接口,操作上也较为方便。过去常见的配置文件是以“.ini”为后缀的文本文件。INI,是英文“初始化(Initial)”的缩写。正如该术语所表示的,INI文件被用来对操作系统或特定程序初始化或进行参数设置。格式上主要分为“节”、“参数”和“注解”三部分。后来由于Windows95推出了注册表的概念,INI配置文件在系统中的地位就大不如前了。不过作为应用程序的配置读写来说,INI还是有其牢固的地位的。后来XML的发展也为配置文件提供了一种更好的存储格式。常见到的“config”文件扩展名的文件,也是在C#编程中较为常用的。C#的配置文件在编码阶段,可通过添加新项找到(名为“应用程序配置文件”),添加入项目后名为app.config,程序编译后一般是以“程序名”+“.exe”+“.config”命名的文件出现。config文件的内容与配置相关的分为两大部分,一部分为应用程序的配置节(appSettings),另一部分为连接字符串的配置节(connectionStrings)。可使用C#提供的ConfigurationManager(原先使用ConfigureationSettings,现已弃用)进行读取。ConfigurationManager中比较重要的是AppSettings和ConnectionStrings两个属性,分别负责读取应用程序的配置项(appSettings)和连接字符串的配置项(connectionStrings)。如果要修改配置文件项,需要先通过ConfigurationManager的OpenExeConfigureation()方法读取信息到Configuration类中,再修改其中对应项的信息,然后执行保存,最后如果要将结果调用出来,还需要ConfigurationManager调用RefreshSection()方法进行刷新。以上是对配置文件一些简单的介绍。下面我们来详细看一下配置文件的各种功能和用法。2 INI配置文件2.1 格式INI配置文件以”ini”为文件扩展名,内容格式为:节、参数和注解。每一个INI文件构成都非常类似,由若干段落(section)组成,在每个带括号的标题下面,是若干个以单个单词开头的关键词(keyword)和一个等号,等号右边的就是关键字对应的值(value)。其一般形式如下: Section1 KeyWord1 = Valuel KeyWord2 = Value2 Section2 KeyWord3 = Value3 KeyWord4 = Value42.1.1 节section2.1.2 参数name=value2.1.3 注解注解使用分号表示(;)。在分号后面的文字,直到该行结尾都全部为注解。如:; comment text2.2 操作C操作INI文件使用的是Windows系统自带Win32的API函数WritePrivateProfileString()和GetPrivateProfileString()函数。这二个函数都位于“kernel32.dll”文件中。C中对Win32的API函数的互操作是通过命名空间“System.Runtime.InteropServices”中的“DllImport”特征类来实现的。它的主要作用是指示此属性化方法是作为非托管DLL的输出实现的。2.2.1 读取 DllImport ( kernel32 ) private static extern int GetPrivateProfileString ( string section ,string key , string def , StringBuilder retVal ,int size , string filePath ) ;参数说明:section:INI文件中的段落名称;key:INI文件中的关键字;def:无法读取时候时候的缺省数值;retVal:读取数值;size:数值的大小;filePath:INI文件的完整路径和名称。2.2.2 写入 DllImport ( kernel32 ) private static extern long WritePrivateProfileString ( string section , string key , string val , string filePath ) ; 参数说明:section:INI文件中的段落;key:INI文件中的关键字;val:INI文件中关键字的数值;filePath:INI文件的完整的路径和名称。2.3 实例下面是自行编写的一个读写INI文件的类 class IniFile /绝对路径(默认执行程序目录) public string FilePath get; set; / / 读取ini文件 / / 段落名 / 键 / 缺省值 / 所对应的值,如果该key不存在则返回空值 / 值允许的大小 / INI文件的完整路径和文件名 / DllImport(kernel32) private static extern int GetPrivateProfileString( string section, string key, string defVal, StringBuilder retVal, int size, string filePath); / / 写入ini文件 / / 段落名 / 键 / 值 / INI文件的完整路径和文件名 / DllImport(kernel32) private static extern long WritePrivateProfileString( string section, string key, string val, string filePath); #region 静态方法 public static string ReadVal(string section, string key, string filePath) string defVal = ; StringBuilder retVal = new StringBuilder(); int size = 10240; string rt = ; try GetPrivateProfileString(section, key, defVal, retVal, size, filePath); rt = retVal.ToString(); catch rt = ; return rt; public static bool WriteVal(string section, string key, string val, string filePath) try if (WritePrivateProfileString(section, key, val, filePath) = 0) return false; else return true; catch return false; #endregion #region 对象方法 public string ReadVal(string section, string key) string defVal = ; StringBuilder retVal = new StringBuilder(); int size = 10240; string rt = ; try GetPrivateProfileString(section, key, defVal, retVal, size, this.FilePath); rt = retVal.ToString(); catch rt = ; return rt; public bool WriteVal(string section, string key, string val) try WritePrivateProfileString(section, key, val, this.FilePath); return true; catch return false; #endregion 3 Config配置文件3.1 文件内容层次configuration为根节点,其下可以带有appSettings和connectionStrings两个子节点。另外需要注意的是这两个子节点中配置项的名称和值并不一样,appSettings中为“key”和“value”,connectionStrings中为“name”和“connectionString”。粗略为如下所示:一个完整的config配置文件内容: 3.2 appSettings节的操作config文件中appSettings的读取主要依靠ConfigurationManager类提供的两个属性:AppSettings和ConnectionStrings,通过字符串索引获取对应配置项的内容。config文件中appSettings的读取主要依靠ConfigurationManager类提供的属性:AppSettings,通过字符串索引获取对应配置项的内容。而写入操作,必须要先读取配置到一个特定的配置对象中,再作修改和保存。3.2.1 读取通过ConfigurationManager.AppSettingskey读取例子: public static string GetAppSetting(string key) string s = ; try s = ConfigurationManager.AppSettingskey; catch return s; 3.2.2 以配置对象为单位写入写入需要将配置文件读取到Configuration对象中,然后在其中修改,最后保存。另外注意,Configuration类是没有构造函数的,需要使用ConfigurationManager类的OpenExeConfiguration()方法打开。 打开与 connectionStrings的打开相同1、 打开当前程序的配置文件。Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);2、 打开指定路径的配置文件。ConfigurationManager.OpenExeConfiguration(string exePath) 修改config.AppSettings.Settingskey.Value = value; /修改对应项config.Save(ConfigurationSaveMode.Modified); /保存ConfigurationManager.RefreshSection(appSettings); /刷新配置文件管理类 添加config.AppSettings.Settings.Add(key, value); /添加/后续保存、刷新不累赘 删除config.AppSettings.Settings.Remove(key); /删除/后续保存、刷新不累赘3.2.3 以配置节为单位写入通过获取配置对象的指定节,在节上进行写入操作。下面的修改为例(注:添加和删除也大同小异)AppSettingsSection appSettings = (AppSettingsSection)m_Config.GetSection(appSettings);appSettings.Settingskey.Value = value;m_Config.Save();3.3 connectionStrings节的操作config文件中connectionStrings的读取跟appSettings类似,也要依靠ConfigurationManager类提供的属性,这个属性为ConnectionStrings。写入操作也类同。3.3.1 读取ConfigurationManager.ConnectionStringsname.ToString()3.3.2 写入 打开参照 修改config.ConnectionStrings.ConnectionStringsname.ConnectionString = connectionString;/config.ConnectionStrings.ConnectionStringsname.ProviderName = providerName; /该项可选 添加ConnectionStringSettings connSetting = new ConnectionStringSettings(name, connectionString, providerName); /生成项 config.ConnectionStrings.ConnectionStrings.Add(connSetting); /添加/后续保存、刷新 删除 m_Config.ConnectionStrings.ConnectionStrings.Remove(ConnectionStringSettings item); /通过指定项删除 m_Config.ConnectionStrings.ConnectionStrings.Remove(name); /通过名称删除3.3.3 以配置节为单位写入直接上例子:ConnectionStringsSection connSection = (ConnectionStringsSection)m_Config.GetSection(connectionStrings);connSection.ConnectionStringsname.ConnectionString = connectionString;m_Config.Save();3.4 自定义配置节的操作利用Configuration的GetSection()方法,获取后操作。3.5 实例直接上源代码:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Configuration;namespace TestConfig1 class ConfigMgr /如需要使用ConfigurationManager类还必须在“引用”中,引用System.Configuration /ConfigurationSettings只是针对旧版的兼容(向下兼容),不建议使用 /配置文件 private static Configuration m_Config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); /读取当前程序的配置文件 / / / / / public static string GetAppSetting(string key) string s = ; try s = ConfigurationManager.AppSettingskey; catch return s; public static string GetConnString(string name) string s = ; try if (ConfigurationManager.ConnectionStringsname != null) s = ConfigurationManager.ConnectionStringsname.ToString(); catch return s; / / 定义设置当前或者其他应用程序配置文件中的appSettings节点,通过AppSettings属性 / / keyName / keyValue public static bool SetAppSetting1(string key, string value) try if (m_Config.AppSettings.Settingskey != null) m_Config.AppSettings.Settingskey.Value = value; else m_Config.AppSettings.Settings.Add(key, value); m_Config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection(appSettings); return true; catch return false; / / 定义设置当前或者其他应用程序配置文件中的appSettings节点 / / keyName / keyValue public static void SetAppSetting(string key, string value) AppSettingsSection appSettings = (AppSettingsSection)m_Config.GetSection(appSettings); if (appSettings.Settingskey != null) appSettings.Settingskey.Value = value; m_Config.Save(); else appSettings.Settings.Add(key, value); m_Config.Save(); ConfigurationManager.RefreshSection(appSettings); / / 定义设置当前或者其他应用程序配置文件中的connectionStrings节点,通过ConnectionStrings属性 / / keyName / keyValue public static bool SetConnectionString1(string name, string connectionString, string providerName = System.Data.SqlClient) try if (m_Config.ConnectionStrings.ConnectionStringsname != null) m_Config.ConnectionStrings.ConnectionStringsname.ConnectionString = connectionString; m_Config.ConnectionStrings.ConnectionStringsname.ProviderName = providerName; /m_Config.Save(ConfigurationSaveMode.Modified); else ConnectionStringSettings connSetting = new ConnectionStringSettings(name, connectionString, providerName); m_Config.ConnectionStrings.ConnectionStrings.Add(connSetting); /config.Save(ConfigurationSaveMode.Modified); m_Config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection(connectionStrings); return true; catch return false; / / 定义设置当前或者其他应用程序配置文件中的connectionStrings节点 / / keyName / keyValue public static void SetConnectionString(string name, string connectionString, string providerName = System.Data.SqlClient) /通过获取对应的节来操作实现 /providerName = System.Data.SqlClient ConnectionStringsSection connSection = (ConnectionStringsSection)m_Config.GetSection(connectionStrings); if (connSection.ConnectionStringsname != null) connSection.ConnectionS
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 五谷道场案例讲解
- 细胞周期控制
- 油漆轮胎改造方案(3篇)
- 皮肤科常用药品
- 食堂休闲区装修方案(3篇)
- 专利代理旅游管理方案(3篇)
- 消防弱电主机维修方案(3篇)
- 初中物理功与功率难题解析及练习题
- 新人训成果汇报
- 设备技术概要总结
- 电梯转让协议书范本
- 牛仔裤廓形趋势报告
- 年产2000吨电子级超高纯石英晶体材料制造项目环评报告表
- 2025年秋季开学第一次全体教师大会上校长讲话-:想为、敢为、勤为、善为
- 2025年圣经神学考试试题及答案
- 2025年佳木斯市郊区招聘公益性岗位人员(37人)笔试备考试题附答案详解(基础题)
- 基孔肯雅热医院感染防控
- 2025至2030年中国脚踏板总成市场现状分析及前景预测报告
- 船舶吊臂维修方案(3篇)
- 信息平台造价管理办法
- 2025年福建省中考历史试题含答案
评论
0/150
提交评论