SoFunction
Updated on 2025-04-06

The easiest way to read and write INI files in C#

1. Introduction

INI file, full name: Initialization File (initialization file), is a traditional text configuration file format that is widely used in Windows operating systems and early applications, and is used to store various settings and parameters of the software.

Although XML, JSON or more modern configuration formats such as YAML are gradually occupying the mainstream during development, INI files still have irreplaceable value in specific scenarios due to their concise and clear structure and easy to read and edit.

This article will focus on the C# locale and introduce how to read and write INI files in the most simplified way.

2. Basics of INI files

INI files organize data in the form of key-value pairs and divide it into several logical sections. Its basic structure is as follows:

; Comment line(Start with a semicolon)

[Section1]     ; Paragraph name,Surrounded by square brackets
key1 = value1   ; Key-value pairs,Equal sign separates keys and values
key2 = value2

[Section2]
keyA = valueA
keyB = valueB

in:

Comments: with semicolon (;Lines beginning with ) are considered comments and are used to interpret settings in the file.

Section: Use square brackets ([]) The name of the package identifies a specific configuration area for classification related settings.

Key: A unique identifier defined in each paragraph to represent a specific configuration item.

Value: The data associated with a key can be information represented by a string, number, or other simple text.

3. The simplest way to read and write INI files in C#

In C#, although the .NET framework does not directly provide built-in support for INI files, it can realize the reading and writing of INI files through the following two simple ways:

1. Use the API provided by the namespace

Although named Visual Basic, the library in this namespace is also available in C#.

This includes convenient ways to operate INI files without the need to introduce external libraries or write complex P/Invoke code.

Here is the simplest example:

using ;

// Read INI filespublic string ReadIniValue(string section, string key, string filePath)
{
    return (filePath, section, key, "");
}

// Write to INI filepublic void WriteIniValue(string section, string key, string value, string filePath)
{
    (filePath, section, key, value);
}

// Define auxiliary classesinternal static class IniParser
{
    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
    [DllImport("kernel32")]
    private static extern int WritePrivateProfileString(string section, string key, string val, string filePath);

    internal static string ReadKeyValue(string iniPath, string section, string key, string defaultValue)
    {
        const int MAX_BUFFER = 65535;
        var sb = new StringBuilder(MAX_BUFFER);
        GetPrivateProfileString(section, key, defaultValue, sb, MAX_BUFFER, iniPath);
        return ();
    }

    internal static void WriteKeyValue(string iniPath, string section, string key, string value)
    {
        WritePrivateProfileString(section, key, value, iniPath);
    }
}

2. Utilize third-party libraries

If you want to avoid calling Win32 API directly, or seek richer features and better error handling, you can choose a mature third-party library, such asIniFileParserorNiniwait. These libraries provide object-oriented interfaces that simplify the operation of INI files. The following is a useIniFileParserA brief example of the library:

using IniParser;
using ;

// Read INI filespublic string ReadIniValue(string section, string key, string filePath)
{
    var parser = new FileIniDataParser();
    IniData data = (filePath);
    return data[section][key];
}

// Write to INI filepublic void WriteIniValue(string section, string key, string value, string filePath)
{
    var parser = new FileIniDataParser();
    IniData data = (filePath);
    data[section][key] = value;
    (filePath, data);
}

4. Conclusion

Although INI files are not the preferred configuration format for modern applications, their simplicity still makes it a viable option under specific needs, especially for lightweight, cross-platform projects.

C# development can be usedAPI or third-party libraries provided by namespaces to implement the read and write operations of INI files with the most simplified code.

Both methods can effectively meet the basic INI file processing needs, and you can choose the appropriate method according to the specific needs and preferences of the project.

at last

This is the article about the simplest way to read and write INI files in C#. For more related contents of C# reading and writing INI files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!