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?
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.
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.
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.