Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Win32

Cinchoo - Using Windows registry as configuration source

5.00/5 (2 votes)
25 Jan 2013CPOL3 min read 9.9K   75  
This article show how to consume Windows registry information using Cinchoo Configuration Manager.

Introduction

Cinchoo is the application framework for .NET. One of the main functionality it provides to the users is the application configuration management. Application configuration is the information that 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 run through with sample in consuming Windows registry information using Cinchoo configuration framework. It provides the flexibility of supporting legacy configuration maintained in Windows registry from your .NET application.  

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: 
C#
[ChoRegistryConfigurationSection("applicationSettings", @"HKCU\Software\Test")]
public class ApplicationSettings : ChoConfigurableObject
{
    [ChoPropertyInfo("STREET")]
    public string Street = "25, River Road";

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

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

    [ChoPropertyInfo("ZIP", DefaultValue = "08837")]
    public string Zip;

    [ChoPropertyInfo("BYTE_ZIP", DefaultValue = "1, 0")]
    [ChoMemberRegistryInfo(RegistryValueKind.Binary)]
    [ChoTypeConverter(typeof(ChoByteArrayToStringConverter))]
    public byte[] ByteZip;

    [ChoPropertyInfo("MULTI_VALUE", DefaultValue = "L1, L2")]
    [ChoMemberRegistryInfo(RegistryValueKind.MultiString)]
    [ChoTypeConverter(typeof(ChoArrayToStringConverter))]
    public string[] MultiValue;

    [ChoPropertyInfo("EXP_STRING", DefaultValue = @"%WinDir%\Raj")]
    [ChoMemberRegistryInfo(RegistryValueKind.ExpandString)]
    public string ExpString;

    [ChoPropertyInfo("DWORD_VALUE", DefaultValue = "123")]
    [ChoMemberRegistryInfo(RegistryValueKind.DWord)]
    public int DWordValue;

    [ChoPropertyInfo("QWORD_VALUE", DefaultValue = "64")]
    [ChoMemberRegistryInfo(RegistryValueKind.QWord)]
    public Int64 QWordValue;
}
  1. In order to be configurable object, it must derive from ChoConfigurableObject.
  2. It must be decorated using ChoRegistryConfigurationSectionAttribute, so that this object will use the Windows registry as the configuration source. The two key parameters are, configuration element path (applicationSettings) and location of the registry key (HKCU\Software\Test) where the configuration values are consumed from.
  3. Windows registry supports 6 types of registry values. The above configuration object contains members of each kind declared. This will give an idea of how to use them in your object. 
    • RegistryValueKind.String - A null-terminated string (REG_SZ)
    • RegistryValueKind.ExpandString- A null-terminated string that contains unexpanded references to environment variables, such as %PATH%, that are expanded when the value is retrieved. (REG_EXPAND_SZ)
    • RegistryValueKind.Binary - Binary data in any form (REG_BINARY)
    • RegistryValueKind.DWord - A 32-bit binary number (REG_DWORD)
    • RegistryValueKind.MultiString - An array of null-terminated strings, terminated by two null characters (REG_MULTI_SZ)
    • RegistryValueKind.QWord - A 64-bit binary number (REG_QWORD

Once you have class declared as above, it is now ready to be consumed the registry values as simple as creating object out of it. The sample code as below 

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

If the registry is not found, framework will create them with default values. Then any changes made to this object as well as in the registry will be exchanged automatically.  

That's all. Sample project attached. Download and try it out for yourself. 

Update #1:  

In this section of the article, I'm going to talk about special registry value, REG_EXPAND_SZ. It is a null-terminated string which contains unexpanded references to environment variables. You may be wondering how to get the expanded value of this registry value? Of course, there is a way to get them. Cinchoo framework seamlessly integrate this feature to this storage in order to extract the expanded value of this REG_EXPAND_SZ registry value. Below sample explains how to do it

C#
[ChoRegistryConfigurationSection("applicationSettings", @"HKCU\Software\Test")]
public class ApplicationSettings : ChoConfigurableObject
{
    [ChoPropertyInfo("STREET")]
    public string Street = "25, River Road";

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

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

    [ChoPropertyInfo("ZIP", DefaultValue = "08837")]
    public string Zip;

    [ChoPropertyInfo("BYTE_ZIP", DefaultValue = "1, 0")]
    [ChoMemberRegistryInfo(RegistryValueKind.Binary)]
    [ChoTypeConverter(typeof(ChoByteArrayToStringConverter))]
    public byte[] ByteZip;

    [ChoPropertyInfo("MULTI_VALUE", DefaultValue = "L1, L2")]
    [ChoMemberRegistryInfo(RegistryValueKind.MultiString)]
    [ChoTypeConverter(typeof(ChoArrayToStringConverter))]
    public string[] MultiValue;

    [ChoPropertyInfo("EXP_STRING", DefaultValue = @"%WinDir%\Raj")]
    [ChoMemberRegistryInfo(RegistryValueKind.ExpandString)]
    public string ExpString;

    [ChoIgnoreProperty]
    public string ExpString_ES;

    [ChoPropertyInfo("DWORD_VALUE", DefaultValue = "123")]
    [ChoMemberRegistryInfo(RegistryValueKind.DWord)]
    public int DWordValue;

    [ChoPropertyInfo("QWORD_VALUE", DefaultValue = "64")]
    [ChoMemberRegistryInfo(RegistryValueKind.QWord)]
    public Int64 QWordValue;
}

In the above sample, the variable 'ExpString' is of REG_EXPAND_SZ registry value type. It holds unexpanded registry value. If you take a closer look at the above sample, you will notice that there is an another member variable 'ExpString_ES' defined. This is where the framework stores the expanded value of the corresponding registry value. Please note that the variable naming convention. It should of 'UnexpandedVariableName_ES' (i.e., ExpString_ES) format. Also make sure this new variable is decorated with 'ChoIgnorePropertyAttribute'. You must declare one additional variable for each REG_EXPAND_SZ registry value in this naming convention in order to retrieve expanded value.

Please download the attached v2 sample above as well as latest Cinchoo framework and try for yourself. Thanks.   

Update #2:

The configuration section attribute (i.e., ChoRegistryConfigurationSection) parameters like registry key 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 bin/config folder. This file usually created very first time run your application. Look for xml node named configuration element path (applicationSettings) 

XML
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="registrySectionHandlerTest">
      <section name="applicationSettings" type="Cinchoo.Core.Configuration.ChoDictionarySectionHandler, Cinchoo.Core, Version=1.0.1.6, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de" />
    </sectionGroup>
  </configSections>
  <registrySectionHandlerTest>
    <applicationSettings cinchoo:registryKey="HKCU\Software\Test" xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework" />
  </registrySectionHandlerTest>
</configuration> 

Take a closer look at the 'applicationSettings' XML node, you may notice an attribute 'registryKey'. This attribute controls the registry source information that Cinchoo framework reads and loads them into configuration object members. You can change this parameter anytime. Will require restart of your application in order to take the new parameter 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)