---
title: "How do you read and write INI or configuration-like files manually (without third-party libraries)?"  
description: "How do you read and write INI or configuration-like files manually (without third-party libraries)?"  
author: "ICSM Computer"  
published: 2025-05-12  
updated: 2025-05-27  
canonical: https://www.mindstick.com/forum/161606/how-do-you-read-and-write-ini-or-configuration-like-files-manually-without-third-party-libraries  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 2 minutes  

---

# How do you read and write INI or configuration-like files manually (without third-party libraries)?

How do you read and write INI or [configuration](https://www.mindstick.com/articles/13112/setting-up-the-perfect-configuration-for-your-work-computer)-like [files](https://www.mindstick.com/articles/23302/the-importance-and-advantage-of-keeping-your-important-files-on-the-cloud) manually (without [third](https://yourviews.mindstick.com/story/4957/the-significance-of-the-third-eye-in-hinduism-more-than-just-a-symbol)-[party](https://yourviews.mindstick.com/view/87403/indian-congress-party-secret-relation-with-china) [libraries](https://answers.mindstick.com/qa/49244/which-company-is-ahead-in-classification-of-data-for-libraries-innovations))?

## Replies

### Reply by Anubhav Sharma

To **read and write INI or configuration-like files manually in C#** (without using third-party libraries), you can create a simple parser using `StreamReader`, `StreamWriter`, and `Dictionary<string, Dictionary<string, string>>` to represent sections and key-value pairs.

## INI Format Example

```plaintext
[Database]
Server=localhost
Port=1433
User=admin

[Logging]
Level=Debug
FilePath=logs/app.log
```

## 1. Reading INI Files Manually

```cs
using System;
using System.Collections.Generic;
using System.IO;

public class IniFile
{
    public Dictionary<string, Dictionary<string, string>> Data { get; } = new();

    public void Load(string filePath)
    {
        string? section = null;

        foreach (var line in File.ReadLines(filePath))
        {
            var trimmed = line.Trim();

            if (string.IsNullOrWhiteSpace(trimmed) || trimmed.StartsWith(";") || trimmed.StartsWith("#"))
                continue;

            if (trimmed.StartsWith("[") && trimmed.EndsWith("]"))
            {
                section = trimmed[1..^1].Trim();
                if (!Data.ContainsKey(section))
                    Data[section] = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
            }
            else if (section != null && trimmed.Contains('='))
            {
                var parts = trimmed.Split('=', 2);
                var key = parts[0].Trim();
                var value = parts[1].Trim();
                Data[section][key] = value;
            }
        }
    }

    public string? Get(string section, string key)
    {
        if (Data.TryGetValue(section, out var kv) && kv.TryGetValue(key, out var value))
            return value;

        return null;
    }
}
```

## Usage:

```cs
var ini = new IniFile();
ini.Load("config.ini");

string? server = ini.Get("Database", "Server");
Console.WriteLine(server);  // Outputs: localhost
```

## 2. Writing INI Files Manually

```cs
public void Save(string filePath)
{
    using var writer = new StreamWriter(filePath);

    foreach (var section in Data)
    {
        writer.WriteLine($"[{section.Key}]");
        foreach (var kvp in section.Value)
        {
            writer.WriteLine($"{kvp.Key}={kvp.Value}");
        }
        writer.WriteLine(); // Blank line between sections
    }
}
```

## Usage:

```plaintext
ini.Data["Database"]["Server"] = "127.0.0.1";
ini.Data["Database"]["User"] = "newadmin";
ini.Save("new_config.ini");
```

## Notes and Best Practices

| Feature | Support |
| --- | --- |
| Comments (`;` or `#`) | Skipped |
| Whitespace handling | Trimmed |
| Case-insensitive keys | Yes |
| Section support | Yes |
| Multi-line values | No (simple implementation) |
| Escaped characters | No (extend if needed) |

Let me know if you want extended support for features like multi-line values or type conversion (int, bool, etc.).


---

Original Source: https://www.mindstick.com/forum/161606/how-do-you-read-and-write-ini-or-configuration-like-files-manually-without-third-party-libraries

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
