INI配置文件格式
0x00 INI配置文件简介
配置文件:如果我们的程序没有任何配置文件时,这样的程序对外是全封闭的,一旦程序需要修改一些参数必须要修改程序代码本身并重新编译,根据设计模式中的开闭原则[对扩展开放对修改关闭],我们可以使用配置文件来解决这个问题,把程序中需要改变的参数按照一定的格式写在一个文件中。我们称这样的文件为配置文件。
常见的配置文件有很多种,如INI配置文件,XML配置文件,JSON配置文件,注册表等。今天我们来介绍下INI配置文件。
*.ini文件是Initialization file的缩写,即为初始化文件,是Windows系统配置文件所采用的存储格式,统管Windows的各项配置,现在广泛用于其他应用软件的配置以实现不同用户的需求。
0x01 文件扩展名
配置文件.ini
请注意:我们所讨论的是项目中的配置文件,它是整个项目共用的,所以它要有一个项目使用的文件名,其后缀是.ini。例如:网络配置 network.ini。
当然ini配置文件的后缀名也不一定必须是“.ini”,也可以是“.cfg”、“.conf”或者是“.txt”。
0x02 文件格式
ini配置文件由节、键、值、注解组成。
节
[section]
参数(键=值)
name=value
注解
注解使用分号表示(;)。在分号后面的文字,直到该行结尾都全部为注解。
- 节(section)
所有的参数都是以节(section)为单位结合在一起的。所有的section名称都是独占一行,并且section名字都被方括号包围着([和])。在section声明后的所有parameters都属于该section。一个section没有明显的结束标识符,一个section的开始就是上一个section的结束,或者是文件结束。如:
[section]
- 参数parameter(键=值)
INI所包含的最基本的“元素”就是参数(parameter),每个参数都有一个name和一个value,name和value由等号“=”隔开,name在等号的左边。如:
name=value
- 注解comments
注解(comments)使用分号表示(;),在分号后面的文字,直到该行结尾都全部为注释
;blabla…
0x03 演示实例
如下为一个INI配置实例:
; testsi-UTF8-std.ini : standard UTF-8 test file for SimpleIni automated testing
;
; The number after a section or key is the order that it is defined in this file
; to make it easier to see if it has been written out correctly. This file should
; be loaded with Unicode / MultiKey / MultiLine turned on.
; This comment should be joined on to the one below it about the key
; with no section.
; Key with no section
lonely-key = nosection
another = nosection either
; This key has no value
empty =
; This should be joined with the comment below about japanese.
; Another line which will be un-indented.
; This is a section of keys showing the word Japanese in different syllabies.
[ordered-1]
a-1 = blah
; this is in kanji
japanese-2 = 日本語
; this is in hiragana
japanese-3 = にほんご
[multi-2]
; value a
test = a
; value b
test = b
; value c
test = c
; value d
test = d
[multiline-3]
; This is obviously a multi-line entry
multiline-1 = <<<MULTI
This is a multi-line comment. It
will continue until we have the word MULTI
on a line by itself.
日本語も。
MULTI
; This looks like multi-line, but because the newline following the last
; line is discarded, it will be converted into a single line entry.
another-2 = <<<MULTI
This is not a multiline entry.
MULTI
; If you wanted a multiline entry with a single line, you need to add
; an extra line to it.
another-3 = <<<MULTI
This is a multiline entry.
MULTI
[integer]
dec = 42
hex = 0x2a

4470

被折叠的 条评论
为什么被折叠?



