配置文件的格式主要有ini、xml、config等,现在对这些格式的配置文件的操作(C#)进行简单说明。

INI配置文件操作

  调用系统函数GetPrivateProfileString()和WritePrivateProfileString()等

(1)导入库

  1. [DllImport("kernel32")]
  2. private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
  3. [DllImport("kernel32")]
  4. private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

(2)调用函数读写ini配置文件

  1. //读
  2. StringBuilder strCom = new StringBuilder();
  3. GetPrivateProfileString("串口参数", "端口", "", strCom, , "Setting.ini");
  4.  
  5. //写
  6. WritePrivateProfileString("串口参数", "端口", "COM3", "Setting.ini");

CFG配置文件操作

  配置文件操作组件SharpConfig 是 .NET 的 CFG/INI 配置文件操作组件。

  配置文件示例(sample.cfg):

  1. [General]
  2. # a comment
  3. SomeString = Hello World!
  4. SomeInteger = 10 # an inline comment
  5. SomeFloat = 20.05
  6. ABoolean = true

  C#代码示例:

  1. Configuration config = Configuration.LoadFromFile( "sample.cfg" );
  2. Section section = config["General"];
  3. string someString = section["SomeString"].Value;
  4. int someInteger = section["SomeInteger"].GetValue<int>();
  5. float someFloat = section["SomeFloat"].GetValue<float>();

  上述SharpConfig参考http://www.oschina.net/p/sharpconfig

config配置文件操作

  AppUser.config文件内容如下:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3.  
  4. <appSettings>
  5. <add key="SvrName" value="127.0.0.1"></add>
  6. <add key="DBName" value="WarehouseDB"></add>
  7. <add key="UsrName" value="sa"></add>
  8. <add key="PassWord" value="sa"></add>
  9. </appSettings>
  10.  
  11. <!--连接数据库字符串-->
  12. <connectionStrings>
  13. <add name="DBConnString" connectionString="server=GAOHONGCHEN\SQLEXPRESS;database=egdb;uid=sa;pwd=123456"/>
  14. </connectionStrings>
  15.  
  16. </configuration>

  C#读取AppUser.config文件的DBConnString的核心代码如下:

  1. ExeConfigurationFileMap CfgFileMap = new ExeConfigurationFileMap();
  2. CfgFileMap.ExeConfigFilename = AppUserCfgPath;
  3. Configuration CfgFile = ConfigurationManager.OpenMappedExeConfiguration(CfgFileMap, ConfigurationUserLevel.None);
  4.  
  5. strConnDB = CfgFile.ConnectionStrings.ConnectionStrings["DBConnString"].ConnectionString;

  config配置文件的详细操作请参考以下链接:

XML配置文件操作

  XML配置文件的一般操作请参见我的另一篇博客:

XML文件操作(C#):http://www.cnblogs.com/gaohongchen01/p/4788504.html

配置文件操作(ini、cfg、xml、config等格式)的更多相关文章

  1. Hibernate框架 主配置文件(Hibernate.cfg.xml) 映射配置 说明

    Hibernate.cfg.xml 主配置文件中主要配置:数据库连接信息.其他参数.映射信息! 常用配置查看源码: hibernate-distribution-3.6.0.Final\project ...

  2. Hibernate框架 主配置文件(Hibernate.cfg.xml)基本

    数据库连接参数配置: <?xml version='1.0' encoding='UTF-8'?> <!--表明解析本XML文件的DTD文档位置,DTD是Document Type ...

  3. 解决离线Could not parse configuration:hibernate.cfg.xml错误

    离线使用hibernate tool 生成反向工程,在配置 配置文件完,生成配置文件后,会报出org.hibernate.HibernateException: Could not parse con ...

  4. hibernate--hibernate.cfg.xml常用配置详解

    在hibernate中最重要的两个配置文件就是hibernate.cfg.xml和xxx.hbm.xml文件,前者是一些配置信息,后者是配置表和对象的映射关系,可以通过注解的方式来取代. 本篇文章主要 ...

  5. .NET程序配置文件操作(ini,cfg,config)

    在程序开发过程中,我们一般会用到配置文件来设定一些参数.常见的配置文件格式为 ini, xml, config等. INI .ini文件,通常为初始化文件,是用来存储程序配置信息的文本文件. [Log ...

  6. c#通用配置文件读写类(xml,ini,json)

    .NET下编写程序的时候经常会使用到配置文件.配置文件格式通常有xml.ini.json等几种,操作不同类型配置文件需要使用不同的方法,操作较为麻烦.特别是针对同时应用不同格式配置文件的时候,很容易引 ...

  7. hibernate配置文件hibernate.cfg.xml和.hbm.xml的详细解释

    原文地址:http://blog.csdn.net/qiaqia609/article/details/9456489 hibernate.cfg.xml -标准的XML文件的起始行,version= ...

  8. hibernate配置文件hibernate.cfg.xml的详细解释

    <!--标准的XML文件的起始行,version='1.0'表明XML的版本,encoding='gb2312'表明XML文件的编码方式-->                 <?x ...

  9. hibernate.cfg.xml hibernate 配置文件模板

    <?xml version='1.0' encoding='UTF-8'?> <!--表明解析本XML文件的DTD文档位置,DTD是Document Type Definition ...

随机推荐

  1. EL表达式显示数据取整问题

    第一 插入fmt标签库 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%&g ...

  2. java学习第16天(泛型 增强for)

    泛型概述 是一种把明确类型的工作推迟到创建对象或者调用方法的时候才去明确的特殊的类型.格式: <数据类型> 注意:该数据类型只能是引用类型. 好处:避免了强制类型转换,比如上个实验   S ...

  3. Rendering pipeline overview(读书笔记1 --- Real-Time rendering)

    1. Rendering pipeline的作用就是在给定的虚拟相机.三维物体.光源.着色方程式.纹理等的条件下产生(渲染)二维图像 2. pipeline包含很多个stage,其效率由最慢的stag ...

  4. java小白来报道

    即将开启我的搬码生涯,奋斗吧,少年!

  5. mobx源码解读2

    我们将上节用到的几个类的构造器列举一下吧: function Reaction(name, onInvalidate) { if (name === void 0) { name = "Re ...

  6. 针对bootstrap内联单选框input与文字不能居中对齐的解决办法

    1.html代码 <div > <label class="checkbox-inline first-label"> <input type=&qu ...

  7. flash开发几个问题

    1.跨域请求xml或txt 问题,使用URLLoader,不能跨域使用 2.js和swf交互问题,尽量使用swfobject.js动态创建

  8. c++保留小数问题,如有不足或错误,欢迎指出

    #include<iostream> #include <iomanip> using namespace std; int main() { double a; cin> ...

  9. ICMP Protocol

    [ICMP Protocol] 参考: 1.ICMP Types and Codes:http://www.nthelp.com/icmp.html 2.RFC 792 - Internet Cont ...

  10. requireJS的引用

    main.js: require.config({ paths: { jquery: 'jquery-1.7.2', biz: 'biz', }}); require(['jquery', 'biz' ...