Saving dictionary to settings

EE,

Trying to find out if I'm able to save a dictionary into my settings using Settings.Save(). I'm needing it to be available to me when I restart my app. I've seen various examples of custom classes producing XML dictionaries, but am unfamiliar with XML.

Other than learn XML, any tips on saving a dictionary into my settings? Is it possible?

Thanks!
ak5745Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

daveamourCommented:
Serialisation is generally possible:

http://www.audacs.co.uk/ViewPage.aspx?PageID=493

However I don't think you can serialise a dictionary:

http://stackoverflow.com/questions/495647/serialize-class-containing-dictionary-member
0
ak5745Author Commented:
Found an example on MSDN:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter.aspx

Their example was for a HashTable, so I modified it:

using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

Open in new window


To serialize:
 public void Serialize(Dictionary<string, int> dictionaryIn, string nameOfDictionaryFile)
        {
            // To serialize the dictionary and its key/value pairs,  
            // you must first open a stream for writing. 
            // In this case, use a file stream.
            // nameOfDicitionaryFile is whatever you want the file to be named.
            // Something like "Dictionary1.txt" or "Dictionary1.dat"
            // Must include extention.
            FileStream fs = new FileStream(nameOfDictionaryFile, FileMode.Create);
            
            // Construct a BinaryFormatter and use it to serialize the data to the stream.
            BinaryFormatter formatter = new BinaryFormatter();
            try
            {
                formatter.Serialize(fs, dictionaryIn);
            }
            catch (SerializationException e)
            {
                Console.WriteLine("Failed to serialize. Reason: " + e.Message);
                throw;
            }
            finally
            {
                fs.Close();
            }
        }

Open in new window


To deserialize:
public void Deserialize(string nameOfDictionaryFile, Dictionary<string, int> dictionaryOut)
        {
            // Declare the dictionary reference.
            Dictionary<string, int> dict = new Dictionary<string, int>();

            // Open the file containing the data that you want to deserialize.
            FileStream fs = new FileStream(nameOfDictionaryFile, FileMode.Open);
            try
            {
                BinaryFormatter formatter = new BinaryFormatter();

                // Deserialize the dictionary from the file and 
                // assign the reference to the local variable.
                dict = (Dictionary<string, int>)formatter.Deserialize(fs);
            }
            catch (SerializationException e)
            {
                Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
                throw;
            }
            finally
            {
                fs.Close();
            }
            //add the key/value pairs to dictionary.
            foreach (KeyValuePair<string, int> de in dict)
            {
                dictionaryOut.Add(de.Key, de.Value);
            }
        }

Open in new window


Again... their code, not mine. Overload with the different types of dictionaries you have (<int, string>, <string, string>, etc.).

There might be a better way out there, but this works for me.

Thanks
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ak5745Author Commented:
Links really didn't help me. Had to do more searching to find my answer.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.