Click here to Skip to main content
Click here to Skip to main content

Cinchoo - Using INI file as configuration source

, 25 Jan 2013
Rate this:
Please Sign up or sign in to vote.
This article shows how to use INI file as configuration source using the Cinchoo Configuration Manager.

Introduction 

Cinchoo is an application framework for .NET. One of the main functionality it provides to users is the application configuration management. Application configuration is information that an application reads and/or writes at run-time from the source.   

Please visit jump start article [Cinchoo - Simplified Configuration Manager] for more information about Cinchoo configuration manager.

In this section, I'll show you with sample using INI section information using the Cinchoo configuration framework. It provides the opportunity in using legacy configuration maintained in Windows INI files from your .NET applications.

How to use 

  • Download latest Cinchoo binary here.
  • Open VS.NET 2010 or higher
  • Create a sample VS.NET (.NET Framework 4) Console Application project.
  • Add reference to Cinchoo.Core.dll
  • Use the Cinchoo.Core.Configuration namespace
  • Define a configuration object 'ApplicationSettings' as below.   
[ChoIniConfigurationSection("applicationSettings", "ADDRESS", @"TestIni.ini")]
public class ApplicationSettings : ChoConfigurableObject
{
    [ChoPropertyInfo("STREET", DefaultValue = "1200, MAY ST")]
    public string Street;

    [ChoPropertyInfo("CITY", DefaultValue = "BOSTON")]
    public string City;

    [ChoPropertyInfo("STATE", DefaultValue = "MA")]
    public string State;

    [ChoPropertyInfo("ZIP", DefaultValue = "98760")]
    public string Zip;
} 
  1. In order to be configurable object, it must derive from ChoConfigurableObject.
  2. It must be decorated using ChoIniConfigurationSectionAttribute, so that this object will use the INI file as the configuration source. The three key parameters are, configuration element path (applicationSettings), INI section name (ADDRESS) and INI file name (TestIni.ini). INI file name can be absolute or relative path. If the file not exists, it will be created automatically. Also if the INI section and INI key-values not found, those will be created automatically as well.

Once you have class declared as above, it is now ready use INI file as simple as creating object out of it. Any changes made to this object as well as in the INI file will be exchanged automatically. The sample code as below 

static void Main(string[] args)
{
    ApplicationSettings applicationSettings = new ApplicationSettings();
    ChoConsole.PauseLine();
} 

That's all. A sample project is attached. Download and try for yourself. 

Update:

The configuration section attribute (i.e., ChoIniConfigurationSectionAttribute) parameters like INI section name, INI filename etc. can be adjusted through application configuration file. It avoids rebuilding your binary for each such change. Here is how you can do it.  

Open [appExeName].xml file under the bin/config folder. This file usually created very first time run your application. Look for xml node named configuration element path (applicationSettings).

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="iniSectionHandlerTest">
      <section name="applicationSettings" 
        type="Cinchoo.Core.Configuration.ChoNameValueSectionHandler, 
              Cinchoo.Core, Version=1.0.1.6, Culture=neutral, 
              PublicKeyToken=b7dacd80ff3e33de" />
    </sectionGroup>
  </configSections>
  <iniSectionHandlerTest>
    <applicationSettings cinchoo:iniSectionName="ADDRESS" 
      cinchoo:iniFilePath="TestIni.ini" 
      xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework" />
  </iniSectionHandlerTest>
</configuration>

Take a closer look at 'applicationSettings' XML node, you may notice two attributes 'iniSectionName' and 'iniFilePath'. There two attributes controls the INI source information that Cinchoo framework reads and loads them into configuration object members. You can change these parameters. Will require restart of your application in order to take the new parameters into effect. Hope this helps.

License

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

About the Author

Cinchoo

United States United States
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 25 Jan 2013
Article Copyright 2013 by Cinchoo
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid