
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();
string[] keysInGlobal = Ini.GetKeys("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
| You must Sign In to use this message board. |
|
|
 |
|
 |
Hey,
your lib is really great, I love it!
But I got one issue with your lib - it doesn't delete a category.
I tried it with ::
IniStructure Ini = IniStructure.ReadIni(inipath); Ini.DeleteCategory("Test");
And Test is the name of the category with 1 key and 1 value - but after reaching this line the category even exist in my .ini file
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
 | Nini  Mattman206 | 12:37 1 Aug '06 |
|
 |
There is also a free library called Nini for reading all types of configuration files.
http://nini.sourceforge.net/[^]
I've used it for a couple projects -- it is a lifesaver.
Bart: Look at me, I'm a grad student. I'm 30 years old and I made $600 last year. Marge: Bart, don't make fun of grad students. They've just made a terrible life choice.
|
| Sign In·View Thread·PermaLink | 5.00/5 |
|
|
|
 |
|
 |
if used in multi-byte language env, should modify line 497 to:
StreamReader reader = new StreamReader(IniStream, Encoding.Default);
otherwise, the multi-byte can't be read successfully.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
62d61 < using System; 212,213c211,212 < < --- > > 455a455,458 > > > if (Lines.Length == 0) > return null; 456a460 >
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi kratchkov, I'd really love INI Handler. When I tried to use it into my project on WinCE.NET with .CF, a NotSupportedException was occuring because of SortedList. Do you have another version of INI Handler without SortedList or give me hints/advise to modify it. Thanks.
Sincerely, Wayne
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Sorry but for me it's working. Maybe the error might be on your side?
---------------------- Instead of printing "No errors have been found", a compiler should rather print "None of your errors have been found", which would be, in most cases, true.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
What are you doing? Changing code structure or adding a huge bunch of new functionality?
I've stopped developing this project, and it's nice from you to continue the work. Of cource, feel free to republish it.
---------------------- Instead of printing "No errors have been found", a compiler should rather print "None of your errors have been found", which would be, in most cases, true.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
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;
myfile.loadini(....); myfile.setvalue(.....); myfile.saveini();
I just thought it's easier for the end user to work with the class this way.
what do you think?
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
I believe it's a personal opinion how saving and loading should be done. Maybe it would be a good idea of letting the user choose by implementing both methods.
For the 'user friendly': I agree, the class seems to be some kind of low-level, without big error handling and such. Usually I create an event in my classes to raise the caught errors, what I haven't done here. But, as same as for saving and loading: Much people -> many techniques.
So... make your programs like you want to make them; you cannot be wrong.
cheers
---------------------- Instead of printing "No errors have been found", a compiler should rather print "None of your errors have been found", which would be, in most cases, true.
|
| Sign In·View Thread·PermaLink | 5.00/5 |
|
|
|
 |
|
 |
Could you post the link to your modified version?
As a personal preferrence I would override the constructor by passing a path to the ini file. Then you can just run a save method on the object to update any changes.
Overall a thumbs up to the author for a neat app!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
This is a very useful code, thanks for posting it.
I would very much appreciate the modified code. Please email to me. Thanks.
Andy
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
In Form1.cs, ‘using IniHandler;’ is defined. It must be ‘using cs_IniHandlerDevelop;’ Functionality is nice.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
 | Form  Filip Geens | 22:41 7 Jul '04 |
|
 |
I know, I am a bit of a form design freak, but I think it would be better to use a tree control to show the keys and sections
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Okay, it would be better. But I think that it's just a demo app to show the use of the class. I don't really care for the form.
---------------------- Instead of printing "No errors have been found", a compiler should rather print "None of your errors have been found", which would be, in most cases, true.
|
| Sign In·View Thread·PermaLink | 3.00/5 |
|
|
|
 |
|
|