- Joined
- Feb 22, 2008
- Messages
- 2,404
- Reaction score
- 724
I designed this class to make the process of reading configuration from ini files a little bit faster and easier. First of all you need to download the iniParser library:
The configuration class:
To use it, simple create a new instance of the Configuration class passing in the constructor the config file path.
Assuming this is your ini file:
To read its very simple, just do:
Remembering the names are CASE-SENSITIVE.
You must be registered to see links
The configuration class:
Code:
public class Configuration
{
IniData parsedData;
Dictionary<string, string> settings;
public Configuration(string iniPath)
{
var parser = new FileIniDataParser();
parsedData = parser.LoadFile(Path.Combine(Environment.CurrentDirectory, iniPath));
settings = new Dictionary<string, string>();
// populate configs
var sections = parsedData.Sections.GetEnumerator();
while (sections.MoveNext())
{
var current = sections.Current;
var sectionName = current.SectionName;
// getting all the keys for that section.
var keys = current.Keys.GetEnumerator();
while (keys.MoveNext())
{
var currentKey = keys.Current;
var keyName = currentKey.KeyName;
var keyValue = currentKey.Value;
this.settings.Add(string.Format("{0}.{1}", sectionName, keyName), keyValue);
}
}
}
public string this[string name]
{
get
{
string result = null;
var success = this.settings.TryGetValue(name, out result);
if (success)
{
return result;
}
else
{
return null;
}
}
}
}
To use it, simple create a new instance of the Configuration class passing in the constructor the config file path.
Assuming this is your ini file:
Code:
[section]
key1 = ragezone
Code:
var key1 = configuration["section.key1"];