Introduction
Using Windows APIs to read and write (WritePrivateProfileString
, GetPrivateProfileString
) INI files is easy, but if you have to get or write data on your app many times, it could cause performance issues because the functions load the INI files on each call.
So, this class will help you managing ini data on memory.
Background
This class does not use the classical INI get or put methods. It loads an INI file on initialize, uses Regular Expressions to parse data, and uses a dictionary for managing section names and data.
Regular Expressions:
static readonly Regex regRemoveEmptyLines =
new Regex
(
@"(\s*;[\d\D]*?\r?\n)+|\r?\n(\s*\r?\n)*",
RegexOptions.Multiline | RegexOptions.Compiled
);
static readonly Regex regParseIniData =
new Regex
(
@"
(?<IsSection>
^\s*\[(?<SectionName>[^\]]+)?\]\s*$
)
|
(?<IsKeyValue>
^\s*(?<Key>[^(\s*\=\s*)]+)?\s*\=\s*(?<Value>[\d\D]*)$
)",
RegexOptions.Compiled |
RegexOptions.IgnoreCase |
RegexOptions.IgnorePatternWhitespace
);
Dictionary
type:
Dictionary<string, NameValueCollection> data =
new Dictionary<string,NameValueCollection>();
Actually, I did not search much to see if there is something like this already. I needed it and I wrote it. And, I thought that someone might find this useful.
Using the code
Initializing:
TA_INIDocument ini = new TA_INIDocument();
TA_INIDocument ini = new TA_INIDocument("C:\\sample.ini");
TA_INIDocument ini =
new TA_INIDocument("C:\\sample.ini", Encoding.Unicode);
Stream iniStream;
TA_INIDocument ini = new TA_INIDocument(iniStream);
Stream iniStream;
TA_INIDocument ini =
new TA_INIDocument(iniStream, Encoding.Unicode);
Getting and setting values:
TA_INIDocument iniDoc;
NameValueCollection keysAndValues = iniDoc["sectionName"];
string value = iniDoc["sectionName"]["keyName"];
string value = iniDoc["sectionName", "keyName"];
iniDoc["sectionName"]["keyName"] = "newValue";
iniDoc["sectionName", "keyName"] = "newValue";
object value = iniDoc["sectionName", "keyName", typeof(Int32)];
iniDoc["sectionName", "keyName", typeof(Rectangle)] =
new Rectangle(0, 0, 200, 300);
bool bValue =
iniDoc.GetBoolean("sectionName", "keyName");
bool bValue =
iniDoc.GetBoolean("sectionName", "keyName", true);
iniDoc.SetValue("sectionName", "keyName", bValue);
DateTime dtValue =
iniDoc.GetDateTime("sectionName", "keyName");
DateTime dtValue =
iniDoc.GetDateTime("sectionName", "keyName", DateTime.MaxValue);
iniDoc.SetValue("sectionName", "keyName", dtValue);
Rectangle rtValue =
iniDoc.GetValue<Rectangle>("sectionName", "keyName");
Rectangle rtValue =
iniDoc.GetValue<Rectangle>("sectionName", "keyName", Rectangle.Empty);
iniDoc.SetValue("sectionName", "keyName", rtValue);
Helper functions:
TA_INIDocument iniDoc;
string[] sectionNames = iniDoc.SectionNames;
string[] keyNames = iniDoc.KeyNames("sectionName");
string[] allValues = iniDoc.SectionValues("sectionName");
if (iniDoc.HasSection("sectionName"))
Application.DoEvents();
if (iniDoc.HasKey("sectionName", "keyName"))
Application.DoEvents();
Saving all:
TA_INIDocument iniDoc;
Stream iniStream;
iniDoc.Save(iniStream);
iniDoc.Save(iniStream, Encoding.Unicode);
iniDoc.Save("C:\\sample.ini");
iniDoc.Save("C:\\sample.ini", Encoding.Unicode);