Question

What I have to do in order to loop on INI file sections?
(Means: references to add and methods to use)?

I use C#, .NET framework 4.

Was it helpful?

Solution 3

I've created an library for parsing and manipulating ini-files entirely in C#. You can iterate for each section, and then for each key-value pair in that section. Or you can access a value if you know the name of the key and the name of the section the key is in:

ini["MySection"]["MyKey"] = "MyValue";

You can install it with NuGet (search for ini-parser) or just grab the code here: https://github.com/rickyah/ini-parser

I've actually made it because several libraries out there use WinAPI calls to read and write ini files. It works, but is you have limited operations, and it is not portable: if you happen to use mono, you are out of luck.

Hope it helps.

OTHER TIPS

Here is another solution that I used for processing INI files:

https://github.com/MarioZ/MadMilkman.Ini

Also here is an example how to loop through all the INI file's content with it:

IniFile ini = new IniFile();
ini.Load("Sample.ini");

foreach (IniSection sec in ini.Sections)
    foreach (IniKey key in sec.Keys)
    {
        string sectionName = sec.Name;
        string keyName = key.Name;
        string keyValue = key.Value;

        // ...
    }

If you really would like to use INI files with C#, here you can find good code:

http://www.codeproject.com/csharp/cs_ini.asp

But it is better to use XML files, because there is no function to write or read an INI file in .NET.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top