配置文件操作(ini、cfg、xml、config等格式)
配置文件的格式主要有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配置文件的详细操作请参考以下链接:
- Read/Write App.Config File with .NET 2.0:http://www.codeproject.com/Articles/14744/Read-Write-App-Config-File-with-NET-2-0
- C#读写app.config中的数据:http://www.cnblogs.com/cadlife/articles/2245776.html
- C#读写config配置文件:http://www.cnblogs.com/luxiaoxun/p/3599341.html
XML配置文件操作
XML配置文件的一般操作请参见我的另一篇博客:
XML文件操作(C#):http://www.cnblogs.com/gaohongchen01/p/4788504.html
配置文件操作(ini、cfg、xml、config等格式)的更多相关文章
- Hibernate框架 主配置文件(Hibernate.cfg.xml) 映射配置 说明
Hibernate.cfg.xml 主配置文件中主要配置:数据库连接信息.其他参数.映射信息! 常用配置查看源码: hibernate-distribution-3.6.0.Final\project ...
- Hibernate框架 主配置文件(Hibernate.cfg.xml)基本
数据库连接参数配置: <?xml version='1.0' encoding='UTF-8'?> <!--表明解析本XML文件的DTD文档位置,DTD是Document Type ...
- 解决离线Could not parse configuration:hibernate.cfg.xml错误
离线使用hibernate tool 生成反向工程,在配置 配置文件完,生成配置文件后,会报出org.hibernate.HibernateException: Could not parse con ...
- hibernate--hibernate.cfg.xml常用配置详解
在hibernate中最重要的两个配置文件就是hibernate.cfg.xml和xxx.hbm.xml文件,前者是一些配置信息,后者是配置表和对象的映射关系,可以通过注解的方式来取代. 本篇文章主要 ...
- .NET程序配置文件操作(ini,cfg,config)
在程序开发过程中,我们一般会用到配置文件来设定一些参数.常见的配置文件格式为 ini, xml, config等. INI .ini文件,通常为初始化文件,是用来存储程序配置信息的文本文件. [Log ...
- c#通用配置文件读写类(xml,ini,json)
.NET下编写程序的时候经常会使用到配置文件.配置文件格式通常有xml.ini.json等几种,操作不同类型配置文件需要使用不同的方法,操作较为麻烦.特别是针对同时应用不同格式配置文件的时候,很容易引 ...
- hibernate配置文件hibernate.cfg.xml和.hbm.xml的详细解释
原文地址:http://blog.csdn.net/qiaqia609/article/details/9456489 hibernate.cfg.xml -标准的XML文件的起始行,version= ...
- hibernate配置文件hibernate.cfg.xml的详细解释
<!--标准的XML文件的起始行,version='1.0'表明XML的版本,encoding='gb2312'表明XML文件的编码方式--> <?x ...
- hibernate.cfg.xml hibernate 配置文件模板
<?xml version='1.0' encoding='UTF-8'?> <!--表明解析本XML文件的DTD文档位置,DTD是Document Type Definition ...
随机推荐
- ORACLE常见数据类型详解
1.字符类型 • CHAR:一个定长字符串,当位数不足自动用空格填充来达到其最大长度.如非NULL的CHAR(12)总是包含12字节信息.CHAR字段最多可以存储2,000字节的 信息. • VARC ...
- Rendering pipeline overview(读书笔记1 --- Real-Time rendering)
1. Rendering pipeline的作用就是在给定的虚拟相机.三维物体.光源.着色方程式.纹理等的条件下产生(渲染)二维图像 2. pipeline包含很多个stage,其效率由最慢的stag ...
- 记录js学习之this用法
一直对Javascript中的this都有一种似是而非的感觉,今天突然感觉豁然开朗,特此记录一下. 咱们先看个栗子: <!DOCTYPE html><html><h ...
- 解决ASP.Net第一次访问慢的处理(IIS8)
本篇经验以IIS8,Windows Server 2012R2做为案例. IIS8 运行在 Windows Server 2012 and Windows 8 版本以上的平台上. IIS中应用程序池和 ...
- html5+css 图片自适应
<div style="position:absolute; width:100%; height:100%; z-index:-1; left:0; top:0;"> ...
- AJAX04 JQ的AJAX
一.jQuery中的Ajax 1.jQuery为我们提供了更强大的Ajax封装 $.ajax({}) 可配置方式发起Ajax请求 $.get() 以GET方式发起Ajax请求 $.post() 以PO ...
- Winform API "user32.dll"中的函数
命名空间:System.Runtime.InteropServices /// <summary> /// 该函数检索一指定窗口的客户区域或整个屏幕的显示设备上下文环境的句柄,以后可以在G ...
- java hashCode方法返回值
hashCode 是和内存地址相关的一个整数. HashCode只是在需要用到哈希算法的数据结构中才有用 用途是为了方便快速地查找对象: HashMap 是根据键对象的 HashCode 来进行快速查 ...
- Linux系统yum命令安装软件时保留(下载)rpm包 -- 转载
昨天在部署zabbix监控的时候,遇到一个问题:监控服务器处于无外网状态,在线安装不可用.而某些依赖包度娘或谷姐搜索起来也挺呛人的!于是想到一个办法,利用有外网的服务器的yum命令来安装这些软件,并自 ...
- python 二叉树实现带括号的四则运算(自学的孩子好可怜,不对的地方请轻责)
#!/usr/bin/python #* encoding=utf-8 s = "20-5*(0+1)*5^(6-2^2)" c = 0 top = [0,s[c],0] op = ...