Click here to Skip to main content
11,806,026 members (68,684 online)
Click here to Skip to main content

Ini Handler

, 2 Jun 2004 84.1K 1.3K 50
Rate this:
Please Sign up or sign in to vote.
Permits simple access to ini files


This class (IniHandler) permits Ini file access. The class represents the contents of the ini, and has functions to list, add, remove and rename categories (they would rather be sections, but ... whatever), and can list, create, delete, and modify key-value pairs. The class has static methods to read and write files.

Using the code

The only class is the IniHandler class in which everything is implemented. It does not write every change directly to the file. You rather get all data, then modify it, and at last save it.

The first part handles all the structure:

All data is saved in one System.Collections.SortedList which contains the category names as keys, and all key-value pairs as values, saved as SortedLists too. So this means there are SortedLists in a SortedList. That behaves like an array in an array, but with dynamic bounds and strings as indexers.

I hope you did understand this, it would have been easier to explain in French or German. You can of course list all categories, and all keys in a specified category. When you want to modify or read a value, you need to specify the categoryname and the keyname only.

This code is all you need to write an ini:

IniStructure Ini = new IniStructure();
IniStructure.WriteIni(Ini,"c:\filename.ini","Optional \n multiline \n comment");

The ini c:\filename.ini would then contain:

# Optional
# multiline
# comment


Each time you write, the file is cleared and rewritten from scratch.

To read an ini, it's very similar:

IniStructure Ini = IniStructure.ReadIni("C:\filename.ini");
string[] allCategories = Ini.GetCategories();

// if Global would be a category, this lists all keys:
string[] keysInGlobal = Ini.GetKeys("Global");
// and if testkey would be a key in Global:
string testKeyValue = Ini.GetValue("Global","testkey");

There are also methods to list the categories and the key-value pairs by index, but they are only used to iterate through all categories and keys during saving.

Points of Interest

It's the first time I use SortedLists, and first tried to write this without them, but I'm happy that I found them because it makes the code a lot simpler. There are some things to do, like error handling: because most methods only return bool, you can only know if an error occurred, but no more. Things like incorrect names (no '=', '[', ']' ... in category names etc..) are handled, but that's all. There's sure room for improvement, but, that's always the case, isn't it?


  • April 17th : First version.
  • May 29th : Bugfix - IniStructure.ReadIni(string) can now read read-only ini files because it opens the file with the FileAccess.Read param.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Switzerland Switzerland
I left the Microsoft world, and am now working with linux and a BSD. This switch was a big relief, mostly for my keyboard, which doesn't get slammed again.

A big thank you to those who helped me with their good articles.

You may also be interested in...

Comments and Discussions

Questionopen source licence question Pin
gemsi7-Jan-15 21:54
membergemsi7-Jan-15 21:54 
Questionsections bug Pin
Member 770255324-Feb-11 7:02
memberMember 770255324-Feb-11 7:02 
GeneralDeleteCategory doesn't work to me Pin
cH40z-Lord9-Oct-07 2:22
membercH40z-Lord9-Oct-07 2:22 
GeneralNini Pin
Mattman2061-Aug-06 11:37
memberMattman2061-Aug-06 11:37 
Generalencoding problem. Pin
margiex27-Feb-06 16:17
membermargiex27-Feb-06 16:17 
GeneralSorting Pin
Cemeron31-Jan-06 11:39
memberCemeron31-Jan-06 11:39 
GeneralTiny bugfix Pin
Rolf Therkildsen16-Aug-05 20:37
memberRolf Therkildsen16-Aug-05 20:37 
QuestionAnother version without SortedList??? Pin
WayneC3-May-05 22:05
memberWayneC3-May-05 22:05 
Generaldownload error Pin
Barraq31-Jul-04 1:09
sussBarraq31-Jul-04 1:09 
GeneralRe: download error Pin
kratchkov1-Aug-04 20:38
memberkratchkov1-Aug-04 20:38 
GeneralImprovement Pin
Alex Rovner29-Jul-04 13:22
memberAlex Rovner29-Jul-04 13:22 
GeneralRe: Improvement Pin
kratchkov1-Aug-04 20:38
memberkratchkov1-Aug-04 20:38 
GeneralRe: Improvement Pin
Alex Rovner2-Aug-04 5:34
memberAlex Rovner2-Aug-04 5:34 
I'm not adding new functionality, since i thought that the class already does what it needs to do which is manage the .ini files.

What im doing is making the class more user friendly, lets say a user wants to add something in a section that doesnt exist, so i'm changing it to create that section, instead of returning null.

Also i rewriten the way you create an instance of the class and the way you load and save files, since i thought it was a bit akwardly done (from how i'm used to do it, maybe you are actually creating it the "right" way.)

Now to create an instance you do:

INIHandler myfile = new INIHandler;


I just thought it's easier for the end user to work with the class this way.

what do you think?
GeneralRe: Improvement Pin
kratchkov4-Aug-04 8:56
memberkratchkov4-Aug-04 8:56 
GeneralRe: Improvement Pin
quintonviljoen22-Dec-04 19:38
memberquintonviljoen22-Dec-04 19:38 
GeneralRe: Improvement Pin
AndyYerg1-Mar-07 8:24
memberAndyYerg1-Mar-07 8:24 
Generalsmall mistake Pin
Filip Geens7-Jul-04 22:00
memberFilip Geens7-Jul-04 22:00 
GeneralRe: small mistake Pin
lokeshdeshmukh15-Jun-08 23:33
memberlokeshdeshmukh15-Jun-08 23:33 
GeneralForm Pin
Filip Geens7-Jul-04 21:41
memberFilip Geens7-Jul-04 21:41 
GeneralRe: Form Pin
kratchkov8-Jul-04 10:10
memberkratchkov8-Jul-04 10:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151002.1 | Last Updated 3 Jun 2004
Article Copyright 2004 by kratchkov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid