Laileb.IniFile 2.0.4

dotnet add package Laileb.IniFile --version 2.0.4
                    
NuGet\Install-Package Laileb.IniFile -Version 2.0.4
                    
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="Laileb.IniFile" Version="2.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Laileb.IniFile" Version="2.0.4" />
                    
Directory.Packages.props
<PackageReference Include="Laileb.IniFile" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Laileb.IniFile --version 2.0.4
                    
#r "nuget: Laileb.IniFile, 2.0.4"
                    
#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.
#:package Laileb.IniFile@2.0.4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Laileb.IniFile&version=2.0.4
                    
Install as a Cake Addin
#tool nuget:?package=Laileb.IniFile&version=2.0.4
                    
Install as a Cake Tool

๐Ÿ’พ IniFile

A lightweight .NET library for reading and writing Windows INI files via the native kernel32 API.

Release NuGet NuGet Downloads Last Commit .NET 10 Windows License


๐Ÿ“‹ Table of Contents


๐Ÿ“– About

IniFile is a thin, zero-dependency wrapper around the Windows kernel32.dll Private Profile functions (WritePrivateProfileString, GetPrivateProfileString, GetPrivateProfileInt, etc.).

It lets you read and write classic INI configuration files with a simple, strongly-typed C# API โ€” no parsing logic needed.

โš ๏ธ Note: This library uses Windows-only P/Invoke calls and is not cross-platform.


โœจ Features

Method Description
Write โœ๏ธ Write a string value to a key in a given section
ReadString ๐Ÿ“– Read a string value (with optional default)
ReadInt ๐Ÿ”ข Read an integer value (with optional default)
ReadBool โœ… Read a boolean โ€” supports true/false, 1/0, yes/no
GetAllSections ๐Ÿ“‚ List all section names in the file
GetAllDataSection ๐Ÿ“‹ List all key=value pairs in a section
DeleteKey ๐Ÿ—‘๏ธ Remove a specific key from a section
DeleteSection ๐Ÿงน Remove an entire section and its keys
KeyExists ๐Ÿ” Check whether a key exists in a section

๐Ÿš€ Getting Started

๐Ÿ“Œ Prerequisites

  • OS: Windows 10 / 11 or Windows Server 2016+
  • SDK: .NET 10 SDK or later
  • Language: C# 14

๐Ÿ“ฆ Installation

NuGet Package Manager
dotnet add package Laileb.IniFile

Or via the Package Manager Console in Visual Studio:

Install-Package Laileb.IniFile

Or add directly to your .csproj:

<PackageReference Include="Laileb.IniFile" Version="2.0.4" />

๐Ÿ’ก Make sure to enable <AllowUnsafeBlocks>true</AllowUnsafeBlocks> in your .csproj (required by LibraryImport source generation).


๐Ÿ’ก Usage

using Ini = IniFile.IniFile;

var ini = new Ini("config.ini");

// Write values
ini.Write("Host", "localhost", "Database");
ini.Write("Port", "5432", "Database");
ini.Write("Debug", "true", "Logging");

// Read values
string host = ini.ReadString("Host", "Database"); // "localhost"
int port = ini.ReadInt("Port", "Database"); // 5432
bool dbg  = ini.ReadBool("Debug", "Logging"); // true
string miss = ini.ReadString("Missing", "Nope", defaultValue: "N/A"); // "N/A"

// Enumerate
string[] sections = ini.GetAllSections(); // ["Database", "Logging"]
string[] entries  = ini.GetAllDataSection("Database"); // ["Host=localhost", "Port=5432"]

// Check & delete
bool exists = ini.KeyExists("Host", "Database"); // true
ini.DeleteKey("Host", "Database");
ini.DeleteSection("Logging");

๐Ÿ‘‰ See the full working demo in examples/IniFile.Example/Program.cs.


๐Ÿ“š API Reference

๐Ÿ”จ Constructor

public IniFile(string filePath)

Creates a new instance bound to the given file path. The path is resolved to an absolute path internally. Throws ArgumentException if the path is null, empty, or whitespace.

๐Ÿท๏ธ Properties

Property Type Description
FilePath string The fully-qualified path to the INI file.

โš™๏ธ Methods

โœ๏ธ Write
public bool Write(string key, string? value, string? section = null)

Writes a string value. Returns true on success. Creates the file, section, and key if they don't exist. When section is omitted or null, the wrapper uses an empty section name, which Windows serializes as [].

๐Ÿ“– ReadString
public string ReadString(string? key, string? section = null, string defaultValue = "", int bufferSize = 1024)

Returns the value for the key, or defaultValue if not found. When section is omitted or null, key reads use an empty section name, which Windows serializes as []. Passing null for key preserves the native enumeration behavior: key names for a section, or section names when both key and section are null.

๐Ÿ”ข ReadInt
public int ReadInt(string key, string? section = null, int defaultValue = -1)

Returns the integer value for the key, or defaultValue if the key is not found. Malformed numeric values follow the native GetPrivateProfileInt parsing behavior and may return 0 instead of defaultValue. When section is omitted or null, the wrapper uses an empty section name, which Windows serializes as [].

โœ… ReadBool
public bool ReadBool(string key, string? section = null, bool defaultValue = false)

Returns true for "true", "1", "yes"; false for "false", "0", "no" (case-insensitive). Returns defaultValue for anything else. When section is omitted or null, the wrapper uses an empty section name, which Windows serializes as [].

๐Ÿ“‚ GetAllSections
public string[] GetAllSections(int bufferSize = 32768)

Returns an array of all section names. Returns an empty array if the file has no sections.

๐Ÿ“‹ GetAllDataSection
public string[] GetAllDataSection(string section, int bufferSize = 32768)

Returns an array of "key=value" strings for every entry in the given section.

๐Ÿ—‘๏ธ DeleteKey
public bool DeleteKey(string key, string? section = null)

Removes a key and its value. Returns true on success. When section is omitted or null, the wrapper uses an empty section name, which Windows serializes as [].

๐Ÿงน DeleteSection
public bool DeleteSection(string? section = null)

Removes an entire section. Returns true on success. When section is omitted or null, the wrapper deletes the empty [] section.

๐Ÿ” KeyExists
public bool KeyExists(string key, string? section = null)

Returns true if the key exists in the section (including keys with empty values). When section is omitted or null, the wrapper uses an empty section name, which Windows serializes as [].


๐Ÿงช Running Tests

dotnet test

Tests are located in tests/IniFile.Tests/ and use xUnit. They create temporary INI files in the system temp directory and clean up after each run.


๐Ÿ—๏ธ Project Structure

IniFile/
โ”œโ”€โ”€ ๐Ÿ“ src/
โ”‚   โ””โ”€โ”€ ๐Ÿ“ IniFile/              # Library source
โ”‚       โ”œโ”€โ”€ ๐Ÿ“„ IniFile.cs
โ”‚       โ””โ”€โ”€ ๐Ÿ“„ IniFile.csproj
โ”œโ”€โ”€ ๐Ÿ“ tests/
โ”‚   โ””โ”€โ”€ ๐Ÿ“ IniFile.Tests/        # xUnit tests
โ”‚       โ”œโ”€โ”€ ๐Ÿ“„ IniFileTests.cs
โ”‚       โ””โ”€โ”€ ๐Ÿ“„ IniFile.Tests.csproj
โ”œโ”€โ”€ ๐Ÿ“ examples/
โ”‚   โ””โ”€โ”€ ๐Ÿ“ IniFile.Example/      # Console demo app
โ”‚       โ”œโ”€โ”€ ๐Ÿ“„ Program.cs
โ”‚       โ””โ”€โ”€ ๐Ÿ“„ IniFile.Example.csproj
โ”œโ”€โ”€ ๐Ÿ“„ Directory.Build.props      # Shared build settings
โ”œโ”€โ”€ ๐Ÿ“„ Directory.Packages.props   # Central package management
โ”œโ”€โ”€ ๐Ÿ“„ IniFile.slnx               # Solution file
โ””โ”€โ”€ ๐Ÿ“„ README.md

๐Ÿค Contributing

Contributions are welcome! To get started:

  1. ๐Ÿด Fork the repository
  2. ๐ŸŒฟ Create a feature branch (git checkout -b feature/my-feature)
  3. โœ๏ธ Make your changes and add tests
  4. โœ… Run dotnet test to verify everything passes
  5. ๐Ÿ“ฌ Open a Pull Request

๐Ÿ“„ License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.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
2.0.4 98 5/29/2026
2.0.3 124 3/27/2026