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

INI配置文件操作

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

(1)导入库

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

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

//读
StringBuilder strCom = new StringBuilder();
GetPrivateProfileString("串口参数", "端口", "", strCom, , "Setting.ini"); //写
WritePrivateProfileString("串口参数", "端口", "COM3", "Setting.ini");

CFG配置文件操作

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

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

[General]
# a comment
SomeString = Hello World!
SomeInteger = 10 # an inline comment
SomeFloat = 20.05
ABoolean = true

  C#代码示例:

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

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

config配置文件操作

  AppUser.config文件内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration> <appSettings>
<add key="SvrName" value="127.0.0.1"></add>
<add key="DBName" value="WarehouseDB"></add>
<add key="UsrName" value="sa"></add>
<add key="PassWord" value="sa"></add>
</appSettings> <!--连接数据库字符串-->
<connectionStrings>
<add name="DBConnString" connectionString="server=GAOHONGCHEN\SQLEXPRESS;database=egdb;uid=sa;pwd=123456"/>
</connectionStrings> </configuration>

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

ExeConfigurationFileMap CfgFileMap = new ExeConfigurationFileMap();
CfgFileMap.ExeConfigFilename = AppUserCfgPath;
Configuration CfgFile = ConfigurationManager.OpenMappedExeConfiguration(CfgFileMap, ConfigurationUserLevel.None); 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. TCP/IP之蓟辽督师

    真正的知识是深入浅出的,码农翻身" 公共号将苦涩难懂的计算机知识,用形象有趣的生活中实例呈现给我们,让我们更好地理解. 本文源地址:TCP/IP之蓟辽督师 本文续<TCP/IP之大明内 ...

  2. The source attachment does not contain the source for the file  ActionSupport.class 错误

    报错  Syntax error, insert ";" to complete FieldDeclaration 报错   The source attachment does ...

  3. Windows内核原理系列01 - 基本概念

    1.Windows API Windows 应用编程接口(API)是针对WIndwos操作系统用户模式的系统编程接口,包含在WindwosSDK中. 2.关于.NET .NET由一个被称为FCL的类库 ...

  4. MATLAB取余求模

    (1)fix(x) : 截尾取整 >> fix( [3.12 -3.12]) ans =   3   -3 (2)floor(x): 不超过x 的最大整数.(高斯取整) >>  ...

  5. RDD:基于内存的集群计算容错抽象(转)

    原文:http://shiyanjun.cn/archives/744.html 该论文来自Berkeley实验室,英文标题为:Resilient Distributed Datasets: A Fa ...

  6. 常用到的git,mvn,postgres,vim命令总结

    mvn: 打包: mvn package 如果想在打包的时候跳过测试: mvn package -Dmaven.test.skip=true 使用的junit测试框架, 测试: mvn test 如果 ...

  7. 36、重新复习html和css之二

    (1)由于公司是意大利的网段, (2)而且公司的电脑是保密的, (3)文件发不出去, (4)U盘插不进去. (5)而且我们组的项目整体上已经开发完毕,客户暂时没有什么大的需求, 所以如果我不把这些技术 ...

  8. maven本地安装jar包同时生成pom文件

    maven 本地安装jar包:mvn install:install-file -Dfile=本地路径/ojdbc12.jar -DgroupId=com.oracle -DartifactId=oj ...

  9. Exynos 4412 Uboot源码解析

    原文地址:http://www.cnblogs.com/jacklu/p/6226330.html Exynos 4412 Uboot的汇编代码就不贴了,没有的可以私信我. 这是我当时阅读代码时的思维 ...

  10. php阻止网页被用户频繁刷新

    一般情况下,用户浏览网页的速度都是几秒十几秒甚至更长时间刷新一页,但有时候又会遇到网页被恶意快速刷新,从而导致正常用户浏览速度缓慢,如何来解决这个问题呢?可以使用如下代码来实现每ip页面访问数量限制: ...