# INI 文件

INI 文件是一种纯文本配置文件，以键值对的形式存储程序设置，这些键值对可以选择性地归入节中，节名出现在方括号内。该格式起源于早期的 [Microsoft Windows](https://www.edgepedia.org/zh/microsoft-windows)，当时 WIN.INI 和 SYSTEM.INI 等初始化文件用来保存系统和应用程序设置；尽管从未有过正式标准，它至今仍存留在 Git 和 Linux 的 systemd 等工具中。<sup>[1](https://www.complang.tuwien.ac.at/raab/comparison_of_configuration_parsers.pdf)</sup><sup> • </sup><sup>[2](https://www.techtarget.com/it-infrastructure/tip/TOML-vs-INI-Comparing-configuration-file-formats)</sup>

| 关键事实 | 详情 |
|---|---|
| 结构 | 节由 `[section]` 标题标记；条目写作 `key=value`；一个节一直有效，直到下一个节标题或文件末尾<sup>[1](https://www.complang.tuwien.ac.at/raab/comparison_of_configuration_parsers.pdf)</sup> |
| 注释 | 单行注释以 `;` 或 `#` 开头<sup>[1](https://www.complang.tuwien.ac.at/raab/comparison_of_configuration_parsers.pdf)</sup> |
| 大小写敏感性 | 键通常不区分大小写；节名的处理因解析器而异<sup>[1](https://www.complang.tuwien.ac.at/raab/comparison_of_configuration_parsers.pdf)</sup><sup> • </sup><sup>[3](https://docs.python.org/3/library/configparser.html)</sup> |
| 标准化 | 该格式不存在标准；各解析器在重复键、转义和编码等细节上有所不同<sup>[1](https://www.complang.tuwien.ac.at/raab/comparison_of_configuration_parsers.pdf)</sup> |
| Windows API | GetPrivateProfileString 和 WritePrivateProfileString 用于读写 INI 文件；两者仅为兼容 16 位 Windows 而维护，新应用程序被引导使用注册表<sup>[4](https://learn.microsoft.com/windows/win32/api/winbase/nf-winbase-writeprivateprofilestringa)</sup> |
| 现代用户 | Git 配置文件以及 systemd 的单元文件和守护进程配置文件使用源自 INI 的语法<sup>[2](https://www.techtarget.com/it-infrastructure/tip/TOML-vs-INI-Comparing-configuration-file-formats)</sup><sup> • </sup><sup>[5](https://manpages.debian.org/bookworm/systemd/systemd.syntax.7.en.html)</sup> |

## 语法

核心文法很小。文件是一系列行。形如 `[section]` 的行开始一个节，其后每一个 `key=value` 条目都属于该节，直到下一个标题或文件末尾。<sup>[1](https://www.complang.tuwien.ac.at/raab/comparison_of_configuration_parsers.pdf)</sup> 注释以分号或井号开头，一直延伸到行尾。<sup>[1](https://www.complang.tuwien.ac.at/raab/comparison_of_configuration_parsers.pdf)</sup> 键通常不区分大小写，值是字符串；解析器不推断数据类型，因此需要数字或布尔值的应用程序会自行转换文本。<sup>[1](https://www.complang.tuwien.ac.at/raab/comparison_of_configuration_parsers.pdf)</sup><sup> • </sup><sup>[3](https://docs.python.org/3/library/configparser.html)</sup>

一个典型的文件如下所示：

```
[database]
host = localhost
port = 5432

[logging]
level = info
; 注释以分号开头
```

在这个共同核心之外，行为就出现了分歧。Python 的 configparser 默认将节名视为区分大小写，而将键视为不区分大小写，允许 `=` 或 `:` 作为分隔符，并且在如此配置时可以接受没有值的键。<sup>[3](https://docs.python.org/3/library/configparser.html)</sup> Cloanto 实现是依照《Microsoft Windows for Workgroups Resource Kit》中的定义编写的，它指定 ISO 8859-1 编码以获得最大兼容性，对重复的节或键采用后出现者胜出的规则，并支持带引号字符串值内部的 [ANSI C](https://www.edgepedia.org/zh/ansi-c) 风格转义序列。<sup>[6](https://cloanto.com/specs/ini/)</sup> Git 的配置格式增加了带引号的子节名，如 `[remote "origin"]`，节名和变量名保持不区分大小写，并识别包括 `true`、`yes`、`on` 和 `1` 在内的布尔拼写。<sup>[7](https://git-scm.com/docs/git-config/2.13.7.html)</sup> systemd 的语法受 XDG Desktop Entry Specification 启发（后者本身源自 Windows 的 .ini 文件），忽略 `=` 周围的空白，支持反斜杠续行，并接受 `1`、`yes`、`true` 和 `on` 作为肯定设置的布尔拼写。<sup>[5](https://manpages.debian.org/bookworm/systemd/systemd.syntax.7.en.html)</sup>

## Windows 中的历史

该格式的名字来自“初始化”。私有初始化文件可追溯到 [Windows 2](https://www.edgepedia.org/zh/windows-2-0x).0；1.0 版只支持共享的 WIN.INI。<sup>[8](https://legacy.desaware.com/support/documents/files/StgTools.pdf)</sup> 到 [Windows 3.1](https://www.edgepedia.org/zh/windows-3-1) 时，标准初始化文件包括 WIN.INI、SYSTEM.INI、CONTROL.INI 和 PROGMAN.INI，应用程序在它们旁边安装自己的 .ini 文件。<sup>[9](https://www.bitsavers.org/pdf/microsoft/resource_kits/0030-31645_Windows_3.1_Resource_Kit_199202.pdf)</sup> 一份当时的 Windows 编程记述指出了塑造后续发展的权衡：INI 文件基于文本、易于编辑，但它们让硬盘上散落着数百个文件，而且无法以层级结构存储信息。<sup>[10](https://learn.microsoft.com/en-us/archive/msdn-magazine/2000/july/under-the-hood-happy-10th-anniversary-windows)</sup>

**16 位 Windows 中的缓存。** 由于 16 位 Windows 使用协作式多任务，系统可以在内存中将已解析的 INI 文件缓存为一级缓存：对文件的第一次操作会加载并解析它，对同一文件的后续操作则复用内存中的副本，避免磁盘访问。缓存在任务切换、磁盘 I/O 或读取不同文件时被刷新，而任何磁盘 I/O 都会将缓存标记为可能过期，迫使在复用前进行时间戳检查。该方案在 [Windows 3.0](https://www.edgepedia.org/zh/windows-3-0) 中开始出现问题，当时 Windows 可以与 MS-DOS 应用程序以抢占式多任务方式并行运行，竞态条件和文件共享违规随之出现。<sup>[11](https://devblogs.microsoft.com/oldnewthing/20240605-00/?p=109852)</sup>

**向注册表的过渡。** Windows 注册表是为取代分散的配置文件而引入的，它取代了 Windows 3.x 中使用的大多数基于文本的 .ini 文件。<sup>[12](https://github.com/MicrosoftDocs/SupportArticles-docs/blob/main/support/windows-server/performance/windows-registry-advanced-users.md)</sup> 配置文件 API 函数保留了下来，但 Microsoft 将其记录为仅为兼容 16 位 Windows 而提供，并引导新应用程序使用注册表。<sup>[4](https://learn.microsoft.com/windows/win32/api/winbase/nf-winbase-writeprivateprofilestringa)</sup> 兼容性通过重定向实现：系统使用 `HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\IniFileMapping` 下的条目将大多数 .ini 文件引用映射到注册表，因此对 WritePrivateProfileString 的调用可能写入注册表而非任何文件，而函数行为没有任何变化。<sup>[4](https://learn.microsoft.com/windows/win32/api/winbase/nf-winbase-writeprivateprofilestringa)</sup> 当 Windows NT 作为 Windows 3.1 的升级安装时，Control.ini、Progman.ini、System.ini、Win.ini 和 Winfile.ini 中的设置被复制到注册表中。<sup>[13](https://www.bitsavers.org/pdf/microsoft/windows_NT_4.0/69940-0696_Windows_NT_Server_4.0_Concepts_and_Planning_199606.pdf)</sup> 重定向还施加了一个实际的大小上限：配置文件 API 函数无法处理每个条目大于 64 KB 的缓冲区，这是 API 的限制，而非文件格式的限制。<sup>[14](https://learn.microsoft.com/en-us/archive/msdn-magazine/2000/november/windows-2000-registry-latest-features-and-apis-provide-the-power-to-customize-and-extend-your-apps)</sup>

## 解析器与方言

由于不存在标准，每个生态系统都自带遵循自身规则的解析器。<sup>[1](https://www.complang.tuwien.ac.at/raab/comparison_of_configuration_parsers.pdf)</sup> Python 的 configparser 是较丰富实现的代表。它支持特殊的 `DEFAULT` 节，其值适用于所有其他节并用于插值，其中诸如 `%(home_dir)s` 的值会按需替换为另一个选项的值；`ExtendedInterpolation` 提供了更高级的变体，插值也可以完全禁用。<sup>[3](https://docs.python.org/3/library/configparser.html)</sup> 它将所有值存储为字符串，并提供 `getint()`、`getfloat()` 和 `getboolean()` 获取器，最后一个以不区分大小写的方式识别 `yes`/`no`、`on`/`off`、`true`/`false` 和 `1`/`0`。<sup>[3](https://docs.python.org/3/library/configparser.html)</sup> 在严格模式下（自 Python 3.2 起为默认），单个源中的重复节或选项会引发 `DuplicateSectionError` 或 `DuplicateOptionError`。<sup>[3](https://docs.python.org/3/library/configparser.html)</sup>

各方言在禁止的内容上也有所不同。Cloanto 规范要求包含逗号或分号的字符串值必须加引号，并将行尾的反斜杠视为续行字符。<sup>[6](https://cloanto.com/specs/ini/)</sup> Git 的格式指出，值后面的行内注释不是 Git 的标准行为，并建议使用整行注释。<sup>[15](https://github.com/gopasspw/gitconfig/blob/master/CONFIG_FORMAT.md)</sup> 常与 INI 一同提及的 Java .properties 格式是一种更严格的扁平键值列表，没有节。<sup>[1](https://www.complang.tuwien.ac.at/raab/comparison_of_configuration_parsers.pdf)</sup>

## INI 与其他配置格式的比较

与 JSON、YAML 和 TOML 相比，INI 以表现力换取简单性。它的限制比 TOML、JSON 或 YAML 更多，但 INI 文件通常更易于编写、编辑和排查问题。<sup>[2](https://www.techtarget.com/it-infrastructure/tip/TOML-vs-INI-Comparing-configuration-file-formats)</sup> INI 不像 XML 那样处理复杂数据结构，嵌套是它的弱点。<sup>[2](https://www.techtarget.com/it-infrastructure/tip/TOML-vs-INI-Comparing-configuration-file-formats)</sup> JSON 是为数据交换而非配置而设计的，根本没有注释语法。<sup>[16](https://yutils.jdgrid.com/en/guides/how-config-formats-work)</sup>

2013 年的一项配置解析器调查用一行话记录了该格式的主要特性：组织为节的键值对结构、单行注释和不区分大小写的键。<sup>[1](https://www.complang.tuwien.ac.at/raab/comparison_of_configuration_parsers.pdf)</sup>

## 当前使用

INI 在其 Windows 起源之外仍被积极使用。包括 Git 和 Linux 的 systemd 在内的现代工具仍在使用该格式。<sup>[2](https://www.techtarget.com/it-infrastructure/tip/TOML-vs-INI-Comparing-configuration-file-formats)</sup> Git 从系统级文件、诸如 `~/.gitconfig` 的每用户文件以及每仓库的 `.git/config` 读取配置，最后找到的值优先。<sup>[17](https://git-scm.com/docs/git-config/2.47.0.html)</sup> systemd 将其源自 INI 的语法应用于单元文件、网络文件和守护进程配置文件。<sup>[5](https://manpages.debian.org/bookworm/systemd/systemd.syntax.7.en.html)</sup> 在 Windows 本身上，该格式存留在诸如 `desktop.ini` 之类的地方，后者存储每文件夹的显示设置，并且必须带有系统和隐藏属性，以免妨碍用户。<sup>[18](https://devblogs.microsoft.com/oldnewthing/20030827-00/?p=42763)</sup>

## References

1. [Comparison of Configuration File Parsers and Systems (Raab, TU Wien)](https://www.complang.tuwien.ac.at/raab/comparison_of_configuration_parsers.pdf)
2. [TOML vs. INI: Comparing configuration file formats, TechTarget](https://www.techtarget.com/it-infrastructure/tip/TOML-vs-INI-Comparing-configuration-file-formats)
3. [configparser — Configuration file parser, Python 3 documentation](https://docs.python.org/3/library/configparser.html)
4. [WritePrivateProfileStringA function (winbase.h), Microsoft Learn](https://learn.microsoft.com/windows/win32/api/winbase/nf-winbase-writeprivateprofilestringa)
5. [systemd.syntax(7), Debian manpages](https://manpages.debian.org/bookworm/systemd/systemd.syntax.7.en.html)
6. [Cloanto Implementation of INI File Format](https://cloanto.com/specs/ini/)
7. [Git - git-config Documentation, version 2.13.7](https://git-scm.com/docs/git-config/2.13.7.html)
8. [Desaware STG tools documentation](https://legacy.desaware.com/support/documents/files/StgTools.pdf)
9. [Microsoft Windows 3.1 Resource Kit (1992), bitsavers](https://www.bitsavers.org/pdf/microsoft/resource_kits/0030-31645_Windows_3.1_Resource_Kit_199202.pdf)
10. [Under the Hood: Happy 10th Anniversary, Windows, MSDN Magazine (2000)](https://learn.microsoft.com/en-us/archive/msdn-magazine/2000/july/under-the-hood-happy-10th-anniversary-windows)
11. [How 16-bit Windows cached INI files for performance, The Old New Thing (2024)](https://devblogs.microsoft.com/oldnewthing/20240605-00/?p=109852)
12. [Windows registry information for advanced users, Microsoft Support](https://github.com/MicrosoftDocs/SupportArticles-docs/blob/main/support/windows-server/performance/windows-registry-advanced-users.md)
13. [Windows NT Server 4.0 Concepts and Planning (1996), bitsavers](https://www.bitsavers.org/pdf/microsoft/windows_NT_4.0/69940-0696_Windows_NT_Server_4.0_Concepts_and_Planning_199606.pdf)
14. [Windows 2000 Registry: Latest Features and APIs, MSDN Magazine (2000)](https://learn.microsoft.com/en-us/archive/msdn-magazine/2000/november/windows-2000-registry-latest-features-and-apis-provide-the-power-to-customize-and-extend-your-apps)
15. [Git Configuration File Format, gopasspw gitconfig](https://github.com/gopasspw/gitconfig/blob/master/CONFIG_FORMAT.md)
16. [YAML vs JSON vs TOML vs XML — Picking a Config Format, yutils](https://yutils.jdgrid.com/en/guides/how-config-formats-work)
17. [Git - git-config Documentation, version 2.47.0](https://git-scm.com/docs/git-config/2.47.0.html)
18. [Why are these unwanted files/folders opening when I log on?, The Old New Thing (2003)](https://devblogs.microsoft.com/oldnewthing/20030827-00/?p=42763)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Data formats and serialization › Text, encodings, and archives

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/ini-file

English original: https://www.edgepedia.org/ini-file
