IniDocument Class
Definition
- Namespace
- Bodu.Text.Configuration
- Assembly
- Bodu.Text.Configuration.dll
- Package
- Bodu.Text.Configuration 1.0.0
- Source
- IniDocument.cs
Represents a mutable INI-style document, providing access to the global section and all named sections, together with a mutation surface for authoring documents programmatically.
public sealed class IniDocument : IniDocumentBase
- Inheritance
-
IniDocument
- Inherited Members
- Extension Methods
Examples
var doc = new IniDocument();
IniSection db = doc.GetOrAddSection("database");
db.SetEntry("host", "localhost");
// Walk every named section.
foreach (IniSection section in doc.Sections)
Console.WriteLine($"[{section.Name}] - {section.Entries.Count} entries");
// Read the global section (keys that appeared before any header).
IniSection global = doc.GlobalSection;
// Lookup by section name.
if (doc.TryGetSection("database", out IniSection? found))
Console.WriteLine(found!["host"]);
Remarks
Build an instance from scratch using the constructors and the AddSection(IniSection)/GetOrAddSection(string) members; a parsed document is obtained through Parse(string, ConfigurationParseOptions?), which exposes the same base read surface.
Section lookup uses ordinal comparison, case-insensitive by default and case-sensitive when the document was created with that option.
Constructors
IniDocument(IniSection, IEnumerable<IniSection>, bool)
Initializes a new instance of the IniDocument class from a global section and an ordered sequence of named sections. Use this constructor to build documents programmatically without first parsing configuration text.
public IniDocument(IniSection globalSection, IEnumerable<IniSection> sections, bool caseSensitiveSections = false)
Parameters
globalSectionIniSectionThe global section (entries authored before the first named section header). Its Name must be the empty string.
sectionsIEnumerable<IniSection>The ordered, named sections that follow the global section.
caseSensitiveSectionsbooltrue to compare section names with ordinal case sensitivity; otherwise, false (the INI default).
Exceptions
- ArgumentNullException
Thrown when
globalSectionorsectionsis null.- ArgumentException
Thrown when
globalSectionhas a non-empty Name, or whensectionscontains a null entry.
IniDocument(bool)
Initializes a new instance of the IniDocument class with an empty global section and no named sections.
public IniDocument(bool caseSensitiveSections = false)
Parameters
Methods
AddSection(IniSection)
Appends section to the document.
public void AddSection(IniSection section)
Parameters
sectionIniSectionThe section to append.
Exceptions
- ArgumentNullException
sectionis null.- ArgumentException
sectionhas an empty Name (i.e. it is the global section).
GetOrAddSection(string)
Returns the first section with the supplied name, creating and appending one when no match exists.
public IniSection GetOrAddSection(string name)
Parameters
namestringThe section name.
Returns
- IniSection
The matching or newly created section.
Exceptions
- ArgumentNullException
nameis null.- ArgumentException
nameis empty.
RemoveSection(string)
Removes the section with the supplied name. When multiple sections share the same name (under Preserve or MergeAdjacent ), every occurrence is removed.
public bool RemoveSection(string name)
Parameters
namestringThe section name to remove.
Returns
Exceptions
- ArgumentNullException
nameis null.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |