Home > Back-end >  ini-parser add key in a for loop
ini-parser add key in a for loop

Time:07-14

hi can anyone please help me? im trying to add a some keys with ini-parser, but i only get the last index of the loop.

        var parser = new FileIniDataParser();
        IniData data = parser.ReadFile("devreorder.ini");

        foreach (DeviceInstance di in Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly))
        {
           
            data["ALL"].AddKey("\""   di.ProductName   "\"", "{"   di.InstanceGuid   "}");
            
        }
        parser.WriteFile("devreorder.ini", data);

i could put the WriteFile in the loop, but that feels wrong to me.

CodePudding user response:

One thing that could really help you debug is to go from DeviceInstance enumeration to List<KeyValuePair<string, string>>, and then in a separate loop use the list of pairs to update the INI file.

var devenum = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly);
var keyvalues = devenum.Select(di => new KeyValuePair<string, string>("\""   di.ProductName   "\"("   devnum   ")", "{"   di.InstanceGuid   "}")).ToList();
     
//readfile here   
foreach (var kvp in keyvalues)
{
    data["ALL"].AddKey(kvp.Key, kvp.Value);        
}
//writefile here

The first part could even be done before ever touching the file. And you will be able to inspect the list contents in the debugger, then if something is wrong, you won't blame the INI parser.

I suspect your problem stems from devnum being the same for all devices. If that's the issue, it will be clearly shown in keyvalues.

  • Related