views:

537

answers:

3

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);
    }
}
+1  A: 

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
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
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
Thanks, looks like a missed a brace out, should be ok now.
Nick
A: 
  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
A: 

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.

João Angelo