Subscribe by Email

Your email:

Posts by Month

Follow Me

Current Articles | RSS Feed RSS Feed

Storing settings in C# Using Nini .NET Configuration Library

 

When developing a desktop application, there will be times when you want to store settings for your program. A database is one option, but on Windows, you might just wish to have your settings stored in an INI file. One way to work with an INI file in C# is with the Nini Library. This makes it quite easy to read from and write to an INI file.

Let’s get started.

After installing the library, we’ll need to set our namespace.

describe the image

What will our INI file look like? Something like this:

INI File

For my application, I decided to make a class devoted to the configuration file. So, let’s define that and a few other variables.

Define variables

Now that we have our variables declared, let’s create a couple of useful methods.

Creating methods

These two methods will update the INI file with new settings, should we change them in our program. Of course, if we make these changes, they need to be saved. Thankfully, we can declare something in our constructor (which we will write a little later) that will auto-save our changes as we make them.

set autosave

Now, let’s create a pair of methods to return the data. This will be useful in our program when we need to use these settings.

methods to return the data

With these methods, we now have a basic class for handling a configuration file. All that is left is our constructor.

But before we get to the constructor, there is something else I created. What if our INI file doesn’t exist? I decided that I would make a function to create a default INI file, should the old one not exist anymore. This is also useful if we want to distribute our program without an INI file.

create a default INI file

We can do a check when we initialize our class that will check to see whether or not this file exists. If not, we’ll create it so we can work with it.

That makes this our constructor:

making the constructor

Our whole class thus looks like this:

the whole class

That’s how simple it can be to work with your own INI files in C#.

Did you find this useful? Let me know in the comments!

 

 

 

Comments

Hi Josh, what would be the benefit of using an external library to create .ini files against using .net built-in application.config files or even a custom built xml file?
Posted @ Wednesday, February 29, 2012 9:49 AM by rene
Each is a valid way of storing configuration information. Using .NET's built in application.config could work, but this would not necessarily be a good choice for a program being developed for multiple platforms. Building an XML file is another good option, but one that some legacy programmers may not be too familiar with. Creating and working with .ini files is something that some developers may or may not want to use, is a familiar way of storing certain settings (and still quite in use in certain cases), and provides a relatively easy learning curve for developers starting out on various platforms.
Posted @ Wednesday, February 29, 2012 11:33 AM by Josh
i agree with you on .net's app.config, but on using the custom xml it would be very easy using serialization and you would have an object with the settings
Posted @ Wednesday, February 29, 2012 11:37 AM by rene
Well, what I posted above doesn't necessarily mean it's the right solution in every case. I stumbled across the library and had a go with it and found it to be easy to work with, so I figured I would share my experience and hopefully help others that may be using that library (or looking for something similar).
Posted @ Wednesday, February 29, 2012 11:44 AM by Josh
Hi Josh, I just want to ask, the CL variable/member where is declared?, it is different than NL? and there is a lot of different better options, I agree with Rene, we can use the in-build approach from VS, this will create a very straightforward class with our needed members and with an usefull functionality (user settings, this will allow us to change the settings in the file at runtime and get the new value on the application) if you want this more flexible and if we already using an external assembly we can use a configuration class(just I/O operations) from our own and build it on an assembly. This will allow us to share this assembly on any different application environments (Desktop, Web or Mobile) all of this using .NET if we are concern about beginners I think we can stick with the in-build settings approach because this will help us build a very simple class with our needed members without much effort. and be ready for use very simple. we have this same approach for any of the platforms.
Posted @ Wednesday, February 29, 2012 12:02 PM by Ramiro
Wow, I didn't catch that. Yes, CL was meant to be NL. Thanks for catching that! 
 
And as I said above, this wasn't meant to be the best solution in all use cases. This was simply a post about using this library after I had stumbled upon it and thought it was interesting. 
 
Not all developers are used to working with .NET or find working with .NET / Visual Studio to be a simple process. This post just shows what can be done, not what must be done. This was an informative post.
Posted @ Wednesday, February 29, 2012 12:27 PM by Josh
Hehe! in that case is good to know what is out there. and yes you are right not everyone of us are using .NET but I get confused when saw that the Nini was for .NET 

 
I've seen that library let us put the configuration on XML (most recently used than INI files), in a very similar way to .NET built-in (correcting the working in my previous comment hehe!) settings providers: 
 
<Nini> 
<Section Name="MySection"> 
<Key Name="SomeName" Value="Some Value" /> 
<Key Name="SomeInteger" Value="5652" /> 
</Section> 
 
<Section Name="AnotherSection"> 
<Key Name="Another Name" Value="Another Value" /> 
</Section> 
</Nini> 
Posted @ Wednesday, February 29, 2012 12:39 PM by Ramiro
thanks, You have saved my searching time
Posted @ Thursday, November 01, 2012 12:19 AM by alan
Thank you very much for sharing this informative blog it is very useful us in future. 
Click Here for Hire .Net Developer of India
Posted @ Friday, April 12, 2013 2:54 AM by Hire .Net Developer India
Post Comment
Name
 *
Email
 *
Website (optional)
Comment
 *

Allowed tags: <a> link, <b> bold, <i> italics