Click here to Skip to main content
11,929,675 members (54,601 online)
Click here to Skip to main content
Add your own
alternative version


58 bookmarked

API-Less INI File Wrapper

, 5 Nov 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Read, Write, Save INI files without using Windows APIs.


Using Windows APIs to read and write (WritePrivateProfileString, GetPrivateProfileString ) INI files is easy, but if you have to get or write data on your app many times, it could cause performance issues because the functions load the INI files on each call.

So, this class will help you managing ini data on memory.


This class does not use the classical INI get or put methods. It loads an INI file on initialize, uses Regular Expressions to parse data, and uses a dictionary for managing section names and data.

Regular Expressions:

static readonly Regex regRemoveEmptyLines =
    new Regex
        RegexOptions.Multiline | RegexOptions.Compiled

static readonly Regex regParseIniData =
    new Regex
        RegexOptions.Compiled | 
        RegexOptions.IgnoreCase | 

Dictionary type:

Dictionary<string, NameValueCollection> data = 
    new Dictionary<string,NameValueCollection>();

Actually, I did not search much to see if there is something like this already. I needed it and I wrote it. And, I thought that someone might find this useful.

Using the code


//Creates an empty ini document, you can add sections,
//keys and values dynamically. And you can save it anytime.
TA_INIDocument ini = new TA_INIDocument();

//Initializes an ini file, you can change its data and save
//it anytime
TA_INIDocument ini = new TA_INIDocument("C:\\sample.ini");

//Initializes an ini file with encoding. Sometimes ini files
//could have unicode data
TA_INIDocument ini = 
    new TA_INIDocument("C:\\sample.ini", Encoding.Unicode);

//Initializes ini data from stream
Stream iniStream;
TA_INIDocument ini = new TA_INIDocument(iniStream);

//Initializes ini data from stream with encoding
Stream iniStream;
TA_INIDocument ini =
    new TA_INIDocument(iniStream, Encoding.Unicode);

Getting and setting values:

TA_INIDocument iniDoc;

//Getting key_value collection of defined section
NameValueCollection keysAndValues = iniDoc["sectionName"];
//Getting string value of defined key and section
string value = iniDoc["sectionName"]["keyName"];
string value = iniDoc["sectionName", "keyName"];
//Setting string value of defined key
iniDoc["sectionName"]["keyName"] = "newValue";
iniDoc["sectionName", "keyName"] = "newValue";
//Getting and Setting object value of defined key and section
object value = iniDoc["sectionName", "keyName", typeof(Int32)];
iniDoc["sectionName", "keyName", typeof(Rectangle)] =
    new Rectangle(0, 0, 200, 300);
//Getting and setting specific type values excepts string
//Primitive Types (included Decimal and DateTime)
//TA_INIDocument.Get{PrimitiveTypeName}(sectionName, keyName, [defaultValue])

bool bValue = 
    iniDoc.GetBoolean("sectionName", "keyName");

bool bValue = 
    iniDoc.GetBoolean("sectionName", "keyName", true);

iniDoc.SetValue("sectionName", "keyName", bValue);

DateTime dtValue = 
    iniDoc.GetDateTime("sectionName", "keyName");

DateTime dtValue = 
    iniDoc.GetDateTime("sectionName", "keyName", DateTime.MaxValue);

iniDoc.SetValue("sectionName", "keyName", dtValue);
//Other types (required that the type has TypeConverterAttribute)
//TA_INIDocument.GetValue<T>(sectionName, keyName, [T:defaultValue])

Rectangle rtValue =
    iniDoc.GetValue<Rectangle>("sectionName", "keyName");

Rectangle rtValue =
    iniDoc.GetValue<Rectangle>("sectionName", "keyName", Rectangle.Empty);

iniDoc.SetValue("sectionName", "keyName", rtValue);

Helper functions:

//Helper Properties and Functions
TA_INIDocument iniDoc;
//Getting All Section Names
string[] sectionNames = iniDoc.SectionNames;
//Getting All Key Names of a Section
string[] keyNames = iniDoc.KeyNames("sectionName");
//Getting All Values of a Section
string[] allValues = iniDoc.SectionValues("sectionName");
//Check if section name exits
if (iniDoc.HasSection("sectionName"))
//Check if key name exits of specified section
if (iniDoc.HasKey("sectionName", "keyName"))

Saving all:

//Saving all data
TA_INIDocument iniDoc;
Stream iniStream;

iniDoc.Save(iniStream, Encoding.Unicode);
iniDoc.Save("C:\\sample.ini", Encoding.Unicode);


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
Turkey Turkey
No Biography provided

You may also be interested in...

Comments and Discussions

Questiongreat code !!! But.... Pin
ChristianGeiger13-Nov-13 3:03
memberChristianGeiger13-Nov-13 3:03 
AnswerRe: great code !!! But.... Pin
Inlyrib3-Nov-15 3:27
memberInlyrib3-Nov-15 3:27 
GeneralMy vote of 5 Pin
ChristianGeiger7-Nov-13 4:12
memberChristianGeiger7-Nov-13 4:12 
works 10 time faster then API calls
BugCan not load a key that its value contain '=' Pin
Behzad Ebrahimi17-Apr-12 6:22
memberBehzad Ebrahimi17-Apr-12 6:22 
GeneralMinor changes to get it work Pin
Member 46632441-Jun-09 23:13
memberMember 46632441-Jun-09 23:13 
GeneralSection Name Casing Bug Pin
Paul E. Bible1-Dec-08 8:39
memberPaul E. Bible1-Dec-08 8:39 
GeneralRe: Section Name Casing Bug Pin
Tolgahan ALBAYRAK1-Dec-08 11:56
memberTolgahan ALBAYRAK1-Dec-08 11:56 
GeneralSuperb !!! Pin
ashu fouzdar12-Nov-08 20:37
memberashu fouzdar12-Nov-08 20:37 
GeneralVery Nice Pin
Paul Conrad12-Nov-08 12:11
memberPaul Conrad12-Nov-08 12:11 
GeneralRe: Very Nice Pin
Tolgahan ALBAYRAK12-Nov-08 23:44
memberTolgahan ALBAYRAK12-Nov-08 23:44 
Generalcong.!! Pin
Member 413265010-Nov-08 13:04
memberMember 413265010-Nov-08 13:04 
GeneralRe: cong.!! Pin
Tolgahan ALBAYRAK10-Nov-08 13:21
memberTolgahan ALBAYRAK10-Nov-08 13:21 
GeneralTerminal Services and Performance Pin
Member 214447310-Nov-08 11:37
memberMember 214447310-Nov-08 11:37 
GeneralRe: Terminal Services and Performance Pin
Tolgahan ALBAYRAK10-Nov-08 12:18
memberTolgahan ALBAYRAK10-Nov-08 12:18 
QuestionPurpose? Pin
Denis Vuyka10-Nov-08 9:43
memberDenis Vuyka10-Nov-08 9:43 
AnswerRe: Purpose? Pin
Tolgahan ALBAYRAK10-Nov-08 11:22
memberTolgahan ALBAYRAK10-Nov-08 11:22 
GeneralRe: Purpose? Pin
Denis Vuyka10-Nov-08 22:06
memberDenis Vuyka10-Nov-08 22:06 
GeneralRe: Purpose? Pin
Tolgahan ALBAYRAK10-Nov-08 22:56
memberTolgahan ALBAYRAK10-Nov-08 22:56 
Generalregular expressions Pin
E. del Ayre9-Nov-08 21:31
memberE. del Ayre9-Nov-08 21:31 
GeneralRe: regular expressions Pin
Tolgahan ALBAYRAK10-Nov-08 10:31
memberTolgahan ALBAYRAK10-Nov-08 10:31 
QuestionPerformance comparison -- benchmark app? Pin
bolivar1237-Nov-08 5:00
memberbolivar1237-Nov-08 5:00 
AnswerRe: Performance comparison -- benchmark app? Pin
Tolgahan ALBAYRAK7-Nov-08 7:54
memberTolgahan ALBAYRAK7-Nov-08 7:54 
GeneralRe: Performance comparison -- benchmark app? Pin
John Simmons / outlaw programmer8-Nov-08 2:52
mvpJohn Simmons / outlaw programmer8-Nov-08 2:52 
GeneralRe: Performance comparison -- benchmark app? Pin
Tolgahan ALBAYRAK8-Nov-08 3:47
memberTolgahan ALBAYRAK8-Nov-08 3:47 
GeneralRe: Performance comparison -- benchmark app? Pin
Randor 14-Nov-08 9:22
member Randor 14-Nov-08 9:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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
Web01 | 2.8.151126.1 | Last Updated 5 Nov 2008
Article Copyright 2008 by Tolgahan ALBAYRAK
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid