Ploch.IniParser 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Ploch.IniParser --version 1.0.0                
NuGet\Install-Package Ploch.IniParser -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Ploch.IniParser" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Ploch.IniParser --version 1.0.0                
#r "nuget: Ploch.IniParser, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Ploch.IniParser as a Cake Addin
#addin nuget:?package=Ploch.IniParser&version=1.0.0

// Install Ploch.IniParser as a Cake Tool
#tool nuget:?package=Ploch.IniParser&version=1.0.0                

.NET Ini File Parser

Overview

Ploch.IniParser is a library for reading and parsing INI files in .NET.

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

Files can be read by providing file lines:

var lines = await File.ReadAllLinesAsync(filePath)`
var iniFile = IniFileParser.Parse(lines);

... or 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](./blob/main/src/IniParser/KeyData.cs#L9) property.

Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last updated
1.0.2 87 11/3/2024
1.0.1 79 11/3/2024
1.0.0 77 11/3/2024