devto 2026-07-26 원문 보기 ↗
I wrote a single-file INI parser/editor for .NET that preserves formatting using regex instead of dictionaries with JSON support.
I've been experimenting with a different approach to editing INI files in C#.
Most libraries parse the file into dictionaries or objects and then regenerate the entire file when saving. That works well for configuration data, but it usually destroys comments, blank lines, indentation, entry order, or duplicate keys.
Instead, I treat the INI file as plain text and index it with a single regular expression. The parser stores only regex matches and edits the original text directly, so unchanged parts of the file remain byte-for-byte identical.
Path=/bin, Path=/usr/bin → ReadStrings() returns both values.
[Paths]
Path=/bin
Path=/usr/bin
Ordinal, InvariantCulture, case-sensitive or insensitive matching."Hello\nWorld!" ↔ actual multiline text:Hello
World!
Script =
{
#!/bin/sh
echo Hello
echo World
}
Extracted text:
#!/bin/sh
echo Hello
echo World
Config =
{
"timeout": 30,
"retry": 5
}
Read/write it as a string or as an object.
[Network]
Port=8080
Host=localhost
[IniSection("Network")]
class NetworkSettings
{
public string Host { get; set; }
[IniEntry("Port")] public int ConnectionPort { get; set; }
[IniIgnore()] public string Comment { get; set; }
}
IniFile.cs to project.The multiline support was probably the most interesting part. The parser recognizes balanced { ... } blocks using .NET balancing groups, so embedded JSON doesn't get confused with INI section headers.
I'd appreciate any feedback, especially if you spot edge cases I haven't considered.