How do you store and retrieve a StringDictionary in app.config?

3.6k Views Asked by At

I have a list of information in a string dictionary that I want to store in the app.config for my program.

I've created the entry in the "settings" portion of the program called "extentions" but when I go to use "Settings.Default.Extentions" I get an error that it is null.

Is there a trick or something to using this?

private void LoadextList()
{
    listBox1.Items.Clear();
    foreach (KeyValuePair<string, string> kvp in Settings.Default.Extentions)
    {
        listBox1.Items.Add(kvp.Key + "\t" + kvp.Value);
    }
}
3

There are 3 best solutions below

3
Nick On

I don't understand your Extensions problem, but I've used this code before to convert between a string and NameValueCollection - you can easily change it to use a StringDictionary:

    public NameValueCollection StringToNameValueCollection(string KeyValueData, char KeyValueSeparator, char ItemSeparator)
    {
        NameValueCollection nvc = new NameValueCollection();

        // split string into array of key/value pairs
        string[] kvpairs = KeyValueData.Split(new char[]{ItemSeparator});

        // add each pair to the collection
        for (int i = 0; i < kvpairs.Length; i++)
        {
            if (!string.IsNullOrEmpty(kvpairs[i]))
            {
                if (kvpairs[i].Contains(KeyValueSeparator.ToString()))
                {
                    // get the key
                    string k = kvpairs[i].Remove(kvpairs[i].IndexOf(KeyValueSeparator.ToString())).Trim();
                    // get the value
                    string v = kvpairs[i].Remove(0, kvpairs[i].IndexOf(KeyValueSeparator) + 1).Trim();

                    // add key/value if key is valid
                    if (!string.IsNullOrEmpty(k))
                    {
                        nvc.Add(k, v);
                    }
                }
                else
                {
                    // no key/value separator in the pair, so add whole string as key and value
                    nvc.Add(kvpairs[i].Trim(), kvpairs[i].Trim());
                }
            }
        }
        return nvc;
    }

Convert NameValueCollection to string:

    public string NameValueCollectionToString(NameValueCollection nvc, char KeyValueSeparator, char ItemSeparator)
    {
        StringBuilder sb = new StringBuilder();

        for(int i = 0; i < nvc.Count; i++)
        {
            if (i != 0)
            {
                sb.Append(ItemSeparator);
            }
            sb.Append(nvc.Keys[i]);
            sb.Append(KeyValueSeparator);
            sb.Append(nvc[i]);
        }

        return sb.ToString();
    }

I've had to make a couple of changes from the code I used, but it should build. Note that it's not too forgiving - the separator characters cannot appear in either the 'key' or 'value' strings.

0
Crash893 On
  public Dictionary<string,string> StringToDictonary(string KeyValueData, char KeyValueSeparator, char ItemSeparator)
{
    Dictionary<string, string> Dic = new Dictionary<string, string>();

    string[] cells = KeyValueData.Split(ItemSeparator);
    foreach (string cell in cells)
    {
        if (!string.IsNullOrEmpty(cell))
        {
            string[] value = cell.Split(KeyValueSeparator);
            if (!string.IsNullOrEmpty(value[0]) && !string.IsNullOrEmpty(value[1]))
            {
                Dic.Add(value[0], value[1]);
            }
        }
    }
    return Dic;
}

break

    public string dictonaryToString(Dictionary<string, string> nvc, char KeyValueSeparator, char ItemSeparator)
    {
        StringBuilder sb = new StringBuilder();
        bool first = true;
        foreach (KeyValuePair<string,string> kvp in nvc)
        {
            if (!first)
            {
                sb.Append(ItemSeparator);
                first = false;
            }
            sb.Append(kvp.Key);
            sb.Append(KeyValueSeparator);
            sb.Append(kvp.Value);
        }
        return sb.ToString();
    }
}
0
João Angelo On

The following question probably contains useful information:

How to store a HashTable in the usersettings?

The problem you are facing is that the two serialization options available when you use a Settings.settings file and the associated auto-generated class are serialization to XML or serialization to string.

The StringDictionary class does not support neither XML nor string serialization. There are some ways you can workaround this limitation and a few of them are listed on the answers to previously mentioned question.