Ploch.IniParser
1.0.2
dotnet add package Ploch.IniParser --version 1.0.2
NuGet\Install-Package Ploch.IniParser -Version 1.0.2
<PackageReference Include="Ploch.IniParser" Version="1.0.2" />
paket add Ploch.IniParser --version 1.0.2
#r "nuget: Ploch.IniParser, 1.0.2"
// Install Ploch.IniParser as a Cake Addin #addin nuget:?package=Ploch.IniParser&version=1.0.2 // Install Ploch.IniParser as a Cake Tool #tool nuget:?package=Ploch.IniParser&version=1.0.2
Ploch INI Parser Library
Overview
Ploch.IniParser is a small library for reading and parsing INI files in .NET.
It is implemented using .NET Standard 2.0, so it can be used in any .NET project that supports .NET Standard 2.0.
Besides handling of INI files, tt can be also used to parse the .editorconfig
files, which are in the INI format.
Features
- .NET Standard 2.0 library
- Parsing of INI files section
- Handling of comments in the INI file for both sections and entries
- Detection of duplicate sections and entries
Usage
For a full sample see the sample project.
var fileLines = await File.ReadAllLinesAsync(filePath);
var iniFile = IniFileParser.Parse(fileLines);
// ===================================================
// Reading global section contents
// ===================================================
foreach (var comment in iniFile.GlobalSection.Comments) // Comments in the global section
{
Console.WriteLine(comment);
}
foreach (var (key, value) in iniFile.GlobalSection.Entries) // Entries in the global section
{
Console.WriteLine($"{key}={value.Value}");
}
// ===================================================
// Reading ini file sections
// ===================================================
foreach (var section in iniFile.Sections.Select(s => s.Value))
{
Console.WriteLine($"***** Section [{section.Name}] *****");
Console.WriteLine("Comments:");
foreach (var comment in section.Comments) // Comments in the section
{
Console.WriteLine(comment);
}
Console.WriteLine("***** Entries: *****");
foreach (var entry in section.Entries.Select(e => e.Value)) // Entries in the section
{
// Checking if there are any comments for the entry
// Comments are attached to an entry if they are on the same line as the entry or the line above the entry
if (entry.Comments.Any())
{
Console.WriteLine("Entry comments:");
foreach (var comment in entry.Comments)
{
Console.WriteLine(comment);
}
}
Console.WriteLine($"Key data: {entry.Key}={entry.Value}"); // Entry key and value
}
Console.WriteLine("***** Duplicate Entries: *****"); // Duplicate entries in the section
foreach (var duplicateEntry in section.DuplicateEntries)
{
Console.WriteLine($"Duplicate Key data: {duplicateEntry.Key}={duplicateEntry.Value}");
}
Console.WriteLine("-------------------------------------");
}
There is also a way to check if there are any duplicate sections in the file. Those extend sections with the same name,
but they are also places in the DuplicateSections
property of the IniFile
object.
var duplicateSections = iniFile.DuplicateSections;
foreach (var section in duplicateSections)
{
Console.WriteLine($"Duplicate Section [{section.Name}]");
foreach (var entry in section.Entries.Select(e => e.Value))
{
Console.WriteLine($"Key data: {entry.Key}={entry.Value}");
}
}
Reading of INI files
There is a few methods for loading of the INI contents.
File can be read by providing a file path:
var iniFile = await IniFileParser.ParseAsync("my-ini-file.ini");
Using a string contents of the file:
var text = await File.ReadAllTextAsync("my-ini-file.ini");
var iniFile = IniFileParser.Parse(text);
Using file lines:
var lines = await File.ReadAllLinesAsync(filePath)`
var iniFile = IniFileParser.Parse(lines);
Using a stream, for example:
using var stream = File.OpenRead(filePath);
var iniFile = await IniFileParser.ParseAsync(stream);
Comments Handling
Comments that are immediately before a section are stored in the [
IniSection.SectionComments
property.
Those comments are assumed to be directly refering to the section.
All comments within a section are also stored in the [
IniSection.Comments
property.
Comments for an entry (which are either on the same line as the entry, or are immediately above the entry) are stored in
the
KeyData.Comments
property.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.