1

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);
    }
}
ЯegDwight
  • 24,821
  • 10
  • 45
  • 52
Crash893
  • 11,428
  • 21
  • 88
  • 123
  • Can you paste the code / screenshot where you created Extensions? – Grzenio Jun 04 '09 at 15:23
  • I created extentions in the propertys of the project under settings as a collections.special.stringdictonary ( i can post a screen gab if you really really want it) – Crash893 Jun 04 '09 at 15:29
  • I should also mention that i can do this with a string in app.config. Im just confused how to do it with a string dictionary. – Crash893 Jun 04 '09 at 15:30
  • See this elegant solution http://stackoverflow.com/questions/338242/how-do-i-store-a-dictionary-object-in-my-web-config-file – sll Oct 14 '12 at 18:53

3 Answers3

1

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.

Community
  • 1
  • 1
João Angelo
  • 56,552
  • 12
  • 145
  • 147
1

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.

Nick
  • 5,616
  • 10
  • 52
  • 72
  • that would probably get me around the issue. ill give it a try, I still feel like i'm missing soemthing fundimental about not knowing why i cant save that stringdictonary. – Crash893 Jun 04 '09 at 15:52
  • Im sorting it out now but your first method throws an error that not all paths give a return (i can probably figure it out but you might wnat to edit your code for the masses) – Crash893 Jun 04 '09 at 15:54
  • Thanks, looks like a missed a brace out, should be ok now. – Nick Jun 04 '09 at 16:19
0
  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();
    }
}
Crash893
  • 11,428
  • 21
  • 88
  • 123