I just installed ini-parser 2.5.2 (for .NET 4.7 application) and ran into an issue testing it.
My .ini file has entries like this:
[INSTANCES]
NUMVTC=3
VTC=0,3330,ABCD,Descr1
VTC=1,3331,EFGH,Descr2
VTC=2,3332,IJKL,Descr3
To test it, I attempted to parse above section using:
List<string> ls = GetIniSectionData("path-to-some-ini", "instances", "VTC");
Piece of code where exception is thrown:
public static List<string> GetIniSectionData(string iniFilePath, string section, string key)
{
List<string> ls = new List<string>();
var parser = FileIniDataParser();
IniData data = parser.ReadFile(iniFilePath); // barfs here: Parsing exception: Duplicate key 'VTC' found in section 'INSTANCES'
int cnt = data[section].Count;
while(!string.IsNullOrEmpty(data[section][key]))
{
ls.add(data[section][key]);
}
return ls;
}
How do I get around duplicate key-value issue?
Update
After fiddling with ini-parser for some time and realizing it probably can't handle duplicate keys, I used the following using Linq. Not sure how robust it is, i guess I will find out soon!
var vals = File.ReadLines(sMappingFilePath)
.SkipWhile(line => !line.StartsWith("[instances]",
StringComparison.CurrentCultureIgnoreCase))
.Skip(1)
.TakeWhile(line => !string.IsNullOrEmpty(line))
.Select(line => new
{
Key = line.Split('=')[0],
Value = line.Split('=')[1]
});
foreach (var val in vals)
{
string[] saValues = val.Value.Split(',');
if (saValues.Length == 4)
{
int vtcNum = Convert.ToInt16(saValues[0]);
int vtcPort = Convert.ToInt16(saValues[1]);
string vtcName = saValues[2];
string vtcDescr = saValues[3];
...
}
}