C# Lightweight Configuration Manager
Simple, fast and powerful configuration manager for .net.
Build your configurations from:
Load your configuration files from:
Preload (preload.xml) file is allowing us on primary load of configuration files during application start. There are two locations where preload.xml file will be searched:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Simple.Config.ConfigurationManager.Preload" value="preload.xml"/>
</appSettings>
</configuration>
Below you can find preload.xml file used in sample:
<?xml version="1.0" encoding="UTF-8"?>
<ConfigManager>
<namespace name="Simple.Config.ConfigurationManager">
<property name="preload">
<value>first.xml</value>
</property>
</namespace>
</ConfigManager>
In sample we are using two configuration files, first.xml and second.ini. As you could see, Simple.Config is supporting two file types:
Below you can see first.xml:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE ConfigManager SYSTEM "ConfigManager.dtd">
<ConfigManager>
<namespace name="Simple.Config.Sample">
<property name="Prop1">
<value>That's the value of Prop1</value>
</property>
<property name="Prop2">
<value>That's the value of Prop2</value>
</property>
</namespace>
</ConfigManager>
Xml configuration files are following simple approach, you are starting the settings with ConfigManager tag, then you are creating namespaces, their properties and their values. Every property should have one or more values. Below you can see DTD schema file (ConfigManager.dtd) which is defining the template (it allows on intellisense support):
<!ELEMENT ConfigManager (namespace+)>
<!ELEMENT namespace (property+)>
<!ELEMENT property (value+)>
<!ELEMENT value (#PCDATA)>
<!ATTLIST namespace
name NMTOKEN #REQUIRED
>
<!ATTLIST property
name NMTOKEN #REQUIRED
>
And the second configuration file, second.ini:
[First Section]
PropA=val1
PropB=val2;val3
The INI configuration file is a text file, containing the section lines ([section name]) and the set of properties. Every property is build from property name and the set of values (property_name=property_value1[;property_value2;...]).
Console.WriteLine("Simple.Config Sample - Start");
Console.WriteLine();
var configManager = ConfigManager.GetInstance();
var sampleNamespace = configManager.GetNamespace("NConfig.Sample");
Console.WriteLine("Namespace [xml]: " + sampleNamespace.Name);
foreach (var property in sampleNamespace.Properties)
Console.WriteLine("Property: [{0} = {1}]", property.Name, property.Value);
Console.WriteLine();
var iniConfigFile = configManager.Load("second.ini");
var iniNamespace = iniConfigFile.Namespaces.First();
Console.WriteLine("Namespace [ini]: " + iniNamespace.Name);
foreach (var property in iniNamespace.Properties)
Console.WriteLine("Property: [{0} = {1}]", property.Name, property.Value);
Console.WriteLine();
var propertyWithManyValues = iniNamespace.Properties[1];
foreach (var value in propertyWithManyValues.Values)
Console.WriteLine("Property: [{0} = {1}]", propertyWithManyValues.Name, value);
Console.WriteLine();
Console.WriteLine("Simple.Config Sample - End");
Simple.Config Sample - Start
Namespace [xml]: Simple.Config.Sample
Property: [Prop1 = That's the value of Prop1]
Property: [Prop2 = That's the value of Prop2]
Namespace [ini]: First Section
Property: [PropA = val1]
Property: [PropB = val2]
Property: [PropB = val2]
Property: [PropB = val3]
Simple.Config Sample - End
Having trouble with Simple.Config? Raise the issue on https://github.com/spolnik/Simple.Config/issues and we’ll help you sort it out.