Click here to Skip to main content
6,596,602 members and growing! (18,479 online)
Email Password   helpLost your password?
Desktop Development » Files and Folders » Configuration Files     Intermediate

Ini Handler

By kratchkov

Permits simple access to ini files
C#, Windows, .NET 1.0, Dev
Posted:26 Apr 2004
Updated:2 Jun 2004
Views:64,591
Bookmarked:46 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
23 votes for this article.
Popularity: 4.99 Rating: 3.67 out of 5
1 vote, 4.3%
1
1 vote, 4.3%
2
5 votes, 21.7%
3
9 votes, 39.1%
4
7 votes, 30.4%
5

Introduction

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();
Ini.AddCategory("NewSection");
Ini.AddValue("NewSection","NewKey","value");
Ini.ModifyValue("NewSection","NewKey","Newvalue");
IniStructure.WriteIni(Ini,"c:\filename.ini","Optional \n multiline \n comment");

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

# Optional
# multiline
# comment

[NewSection]
NewKey=NewValue

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?

History

  • 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.

License

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

kratchkov


Member
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.
Location: Switzerland Switzerland

Other popular Files and Folders articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 18 of 18 (Total in Forum: 18) (Refresh)FirstPrevNext
GeneralDeleteCategory doesn't work to me PinmembercH40z-Lord3:22 9 Oct '07  
GeneralNini PinmemberMattman20612:37 1 Aug '06  
Generalencoding problem. Pinmembermargiex17:17 27 Feb '06  
GeneralSorting PinmemberCemeron12:39 31 Jan '06  
GeneralTiny bugfix PinmemberRolf Therkildsen21:37 16 Aug '05  
GeneralAnother version without SortedList??? PinmemberWayneC23:05 3 May '05  
Generaldownload error PinsussBarraq2:09 31 Jul '04  
GeneralRe: download error Pinmemberkratchkov21:38 1 Aug '04  
GeneralImprovement PinmemberAlex Rovner14:22 29 Jul '04  
GeneralRe: Improvement Pinmemberkratchkov21:38 1 Aug '04  
GeneralRe: Improvement PinmemberAlex Rovner6:34 2 Aug '04  
GeneralRe: Improvement Pinmemberkratchkov9:56 4 Aug '04  
GeneralRe: Improvement Pinmemberquintonviljoen20:38 22 Dec '04  
GeneralRe: Improvement PinmemberAndyYerg9:24 1 Mar '07  
Generalsmall mistake PinmemberFilip Geens23:00 7 Jul '04  
GeneralRe: small mistake Pinmemberlokeshdeshmukh0:33 16 Jun '08  
GeneralForm PinmemberFilip Geens22:41 7 Jul '04  
GeneralRe: Form Pinmemberkratchkov11:10 8 Jul '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 Jun 2004
Editor: Nishant Sivakumar
Copyright 2004 by kratchkov
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project