0

I'm currently developing a C# Tool for my company and I just got stuck working with the dictionary.

It's about reading from an .ini file and saving the Sections and Keys to a Dictionary<string, string>.

What I have here:

    public void  GetDataToDictrionary()
    {
        FileIniDataParser fileParser = new FileIniDataParser();
        IniData data = fileParser.ReadFile("config.ini");

        Dictionary<string, string> newDictionary = new Dictionary<string, string>();

        foreach (SectionData section  in data.Sections)
        {
            foreach (KeyData key in section.Keys)
                newDictionary.Add(section.SectionName.ToString(), key.Value.ToString());
        }
    }

I know that the newDictionary.Add function will throw some Argument exception.

If I write it directly from the .Ini File to the Console with:

foreach (SectionData section in data.Sections)
{
    Console.WriteLine("[" + section.SectionName + "]");

    foreach (KeyData key in section.Keys)
        Console.WriteLine(key.KeyName + " = " + key.Value);`
}

It will output as:

`*[NewCCReduced1]

name=NewCCReduced1.cone.bombardier.com

ipaddress=10.167.21.11

macaddress=000bab46edeb*`

Which is totally fine as the .ini File has this structure:

`*[NewCCReduced1] /Sectionname

name=NewCCReduced1.cone.bombardier.com /KeyName = KeyValue

ipaddress=10.167.21.11 /KeyName = KeyValue

macaddress=000bab46edeb /KeyName = KeyValue

/Comment*`

Has somebody of you maybe an idea how I could solve the problem like saving all there variables to my Dictionary?

Luca
  • 1,766
  • 3
  • 27
  • 38
  • Thanks to @Sparky, To precise my question, is it possible for me to somehow Itinerate the KeyValues so I won't get the duplication error? So would it work to have a String SectionName and a String KeyNames with the Values name, ipaddress and macaddress? – Luca Sep 10 '14 at 08:13
  • It's not uncommon to find a pre-existing solution, [The top hit on google](http://stackoverflow.com/questions/217902/reading-writing-an-ini-file) – James Sep 10 '14 at 08:15

4 Answers4

1

You've got it wrong. You are trying to create a dictionary that each item will have a key equal to the section name. That will break the rule of unigue keys in a dictionary.

I think that what you want is the following:

Dictionary<string, Dictionary<string,string>> newDictionary = new Dictionary<string, Dictionary<string,string>>();

foreach (SectionData section  in data.Sections)
{
    var keyDictionary = new Dictionary<string,string>();
    foreach (KeyData key in section.Keys)
        keyDictionary.Add(key.KeyName.ToString(),key.KeyValue.ToString());

    newDictionary.Add(section.SectionName.ToString(), keyDictionary);
}

I have not tested the above, but it should work.

Giannis Paraskevopoulos
  • 18,261
  • 1
  • 49
  • 69
1

Hence your call to newDictionary.Add(section.SectionName.ToString(), key.Value.ToString()); is in a loop it will add the section to the dictionary up to n times. You may use the following instead:

foreach (SectionData section  in data.Sections)
{
    newDirctionary.Add(section.SectionName.ToString(), new List<string());
    foreach (KeyData key in section.Keys)
        newDictionary[section.SectionName.ToString()].Add(key.Value.ToString());
}

Be aware that your dictionary (<string, List<string>) has a list as value in order to store more then one single key.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
0

Your code is adding duplicate keys to the dictionary, which is what is generating the exception.

newDictionary.Add(section.SectionName.ToString(), key.Value.ToString());

Since SectionName is the same, you are attempting to put two different values into one dictionary entry, which is not allowed.

Sparky
  • 14,967
  • 2
  • 31
  • 45
0

You are adding the keys twice for this reason you have got the exception To solve your issue you can use a dictionary of dictionary something like the following

 Dictionary<string,  Dictionary<string, string> > newDictionary = new Dictionary<string, Dictionary<string, string> >();

To Handle your config file

   foreach (var section in newDictionary)
        {
            //here you  have the section name as a key 
            foreach (var param in section.Value)
            {
                // here you will got all the params  values  for every section 
            }
        }

the first string is the name of your section as a key the second Dictionary will have a key value of your section config

BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47