using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace StumDE.Misc
{
///
/// Read/Write .ini Files
///
/// Version 1, 2009-08-15
/// http://www.Stum.de
///
///
/// It supports the simple .INI Format:
///
/// [SectionName]
/// Key1=Value1
/// Key2=Value2
///
/// [Section2]
/// Key3=Value3
///
/// You can have empty lines (they are ignored), but comments are not supported
/// Key4=Value4 ; This is supposed to be a comment, but will be part of Value4
///
/// Whitespace is not trimmed from the beginning and end of either Key and Value
///
/// Licensed under WTFPL
/// http://sam.zoy.org/wtfpl/
///
public class IniFile
{
private Dictionary> _iniFileContent;
private readonly Regex _sectionRegex = new Regex(@"(?<=\[)(?[^\]]+)(?=\])");
private readonly Regex _keyValueRegex = new Regex(@"(?[^=]+)=(?.+)");
public IniFile() : this(null){}
public IniFile(string filename)
{
_iniFileContent = new Dictionary>();
if (filename != null) Load(filename);
}
///
/// Get a specific value from the .ini file
///
///
///
/// The value of the given key in the given section, or NULL if not found
public string GetValue(string sectionName, string key)
{
if (_iniFileContent.ContainsKey(sectionName) && _iniFileContent[sectionName].ContainsKey(key))
return _iniFileContent[sectionName][key];
else
return null;
}
///
/// Set a specific value in a section
///
///
///
///
public void SetValue(string sectionName, string key, string value)
{
if(!_iniFileContent.ContainsKey(sectionName)) _iniFileContent[sectionName] = new Dictionary();
_iniFileContent[sectionName][key] = value;
}
///
/// Get all the Values for a section
///
///
/// A Dictionary with all the Key/Values for that section (maybe empty but never null)
public Dictionary GetSection(string sectionName)
{
if (_iniFileContent.ContainsKey(sectionName))
return new Dictionary(_iniFileContent[sectionName]);
else
return new Dictionary();
}
///
/// Set an entire sections values
///
///
///
public void SetSection(string sectionName, IDictionary sectionValues)
{
if (sectionValues == null) return;
_iniFileContent[sectionName] = new Dictionary(sectionValues);
}
///
/// Load an .INI File
///
///
///
public bool Load(string filename)
{
if (File.Exists(filename))
{
try
{
var content = File.ReadAllLines(filename);
_iniFileContent = new Dictionary>();
string currentSectionName = string.Empty;
foreach (var line in content)
{
Match m = _sectionRegex.Match(line);
if (m.Success)
{
currentSectionName = m.Groups["SectionName"].Value;
}
else
{
m = _keyValueRegex.Match(line);
if (m.Success)
{
string key = m.Groups["Key"].Value;
string value = m.Groups["Value"].Value;
Dictionary kvpList;
if (_iniFileContent.ContainsKey(currentSectionName))
{
kvpList = _iniFileContent[currentSectionName];
}
else
{
kvpList = new Dictionary();
}
kvpList[key] = value;
_iniFileContent[currentSectionName] = kvpList;
}
}
}
return true;
}
catch
{
return false;
}
}
else
{
return false;
}
}
///
/// Save the content of this class to an INI File
///
///
///
public bool Save(string filename)
{
var sb = new StringBuilder();
if (_iniFileContent != null)
{
foreach (var sectionName in _iniFileContent)
{
sb.AppendFormat("[{0}]\r\n", sectionName.Key);
foreach (var keyValue in sectionName.Value)
{
sb.AppendFormat("{0}={1}\r\n", keyValue.Key, keyValue.Value);
}
}
}
try
{
File.WriteAllText(filename, sb.ToString());
return true;
} catch
{
return false;
}
}
}
}