0

I am working on a personal C# project and I would like to use a nested dictionary to handle the storing of my settings.ini file values for future reading/writing to my settings.ini file. My settings.ini file consists of:

[Application]  
AutoShowSetting=Log  
SettingsIniDirectory=Application  
LogDirectory=Application  

...

I am using a two for each loops to read the information from the settings.ini file like so:

public void storeIniValues()
{


    int i = 0;
    string[] sectionNames = iniFile.GetSectionNames();

    foreach (string section in sectionNames)
    {
        string[] keys = iniFile.GetKeyNames(section);
        foreach (string key in keys)
        {                  
            string keyValue = iniFile.GetString(section, key, "");
            i++;

        }         

    }
}

The foreach grabs the values and places the values into their respected string variables. I would like to take the values while looping through the foreach loop and place the values into a dictionary to later read/write/update the settings.ini file. So far I created a dictionary:

static Dictionary<string, Dictionary<string, string>> iniSettings =
    new Dictionary<string, Dictionary<string, string>>();

How do I access the ini values from the nested dictionary like so
iniSettings["Application"]["AutoShowSetting"] to get the corresponding value(in this case "Log")? Also, how do I add a value to the nested Dictionary while the foreach loop is looping(settingsIni[section][key] = keyValue)? I have no idea where to begin attempting this because I have never used a nested Dictionary.

EDIT
I forgot to mention the settings.ini file will hold values to populate checkboxes, textboxes, and etc...

dottedquad
  • 1,371
  • 6
  • 24
  • 55
  • 2
    Or you could use the perfectly fine app.config structure already present for C# applications? =) – J. Steen Jul 23 '13 at 08:22
  • But now that you're already here, what's wrong with the solution you propose in your last paragraph? Do you get any errors when you try it? – J. Steen Jul 23 '13 at 08:25
  • 1
    Or use one of the INI wrapper classes as discussed here: http://stackoverflow.com/questions/217902/reading-writing-an-ini-file – Corak Jul 23 '13 at 08:26
  • @J.Steen when implementing: iniSettings[section][key] = keyValue; I receive this error: An unhandled exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll Additional information: The given key was not present in the dictionary. I am assuming I am searching for the value of the key instead of setting the value? – dottedquad Jul 23 '13 at 08:32
  • 1
    You need to actually add the key and value to your dictionary first, by calling `Add(key, value)`. So, say `iniSettings[section].Add(key, value);` assuming you've already added the section by calling `iniSettings.Add(section, new Dictionary());`. – J. Steen Jul 23 '13 at 08:35
  • @J.Steen Add(section, key) will add section and key variable name from my settings.ini file. How do I add he settings.ini file variable name plus the value? I am not sure how to Add for nested Dictionary. **Edit** I just read your edited comment. :-) – dottedquad Jul 23 '13 at 08:42
  • I've added an answer with a bit of a more thorough explanation. If you find you can't construct a solution from that, please don't hesitate to ask for clarification. – J. Steen Jul 23 '13 at 08:44

1 Answers1

1

Assuming you want to continue down this path, rather than using the configurationmanager already built into .NET, or an existing ini-file wrapper as already mentioned in comments, you need to do this to add values to your in-memory structure:

When you've got a section, you need to add it.

iniSettings.Add(section, new Dictionary<string, string>());

When you've got a section (after adding it), a key and a value, you need to add that.

iniSettings[section].Add(key, value);

This will lead to a structure that resembles

+ section1
   + key1, value
   + key2, value
+ section2
   + key1, value

etc. You can check if a section exists in your in-memory structure by calling

bool sectionExists = iniSettings.ContainsKey(section);

which will return a bool. The same goes for keys, of course:

bool keyExists = iniSettings[section].ContainsKey(key);

To access them, you simply use what you've already thought up:

var value = iniSettings[section][key];
J. Steen
  • 15,470
  • 15
  • 56
  • 63
  • 1
    Pardon the judgemental-sounding first paragraph, I just wanted to make it clear there are alternatives. =) – J. Steen Jul 23 '13 at 08:48
  • I'm curious if I am able to go one more Dictionary deep. such as: `static Dictionary, string>> iniSettings = new Dictionary, string>>(); iniSettings.Add(section, new Dictionary, string>());` If that is possible, how do I add to the nested dictionary? – dottedquad Aug 13 '13 at 16:13
  • You can go as deep as you want, just initialise each subsection as I've shown in the code examples above. However, if you want unlimited depth you might want to look into making a dedicated structure of classes for this instead. I'm sure you'll figure it out. =) – J. Steen Aug 13 '13 at 17:37