Take the 2-minute tour ×

I store some string values in an array list at runtime. When the application closes I want to store the data from the array list in another location so that I can open and retrieve the data the next time the application starts. What are some possible ways to do this?

share|improve this question
You really shouldn't ever use ArrayList anymore. You should use List<T> so that you have a strongly typed collection. –  Servy Jul 24 '12 at 17:11
add comment

2 Answers

up vote 1 down vote accepted

You can use Reading and Writing from/to files or storing the values in windows registry. For Reading/Writing from/to files use:

StreamReader sr = new StreamReader(@"C:/store.dll"); //for reading
StreamWriter sw = new StreamWriter(@"C:/store.dll"); //for writing

This is the basic. Here are two great articles for this:

  1. Reding from files
  2. Writing to files

I used this for storing the High Scores for a simple game. :)

Here is a nice tutorial for using Windows registry.

share|improve this answer
Why .dll? Dll is for libraries, and not for data. –  CodesInChaos Jul 24 '12 at 17:02
Just an example. It can be any type of file... :D –  Ionică Bizău Jul 24 '12 at 17:03
Is there any different, more "isolate" way, to save and restore my data? I prefer no to use window registry –  user1005633 Jul 24 '12 at 17:13
You can create a hidden file. :) Is this better? –  Ionică Bizău Jul 24 '12 at 17:14
Yes, maybe i will try this, thanks!! –  user1005633 Jul 24 '12 at 17:16
show 2 more comments

You could Serialize the ArrayList and write it to disk, then later on load the file and Deserialize it to an object:

    public static XDocument Serialize<T>(T objectIn, Type[] extraTypes)
            var target = new XDocument();
            XmlSerializer s = extraTypes != null ? new XmlSerializer(objectIn.GetType(), extraTypes) : new XmlSerializer(objectIn.GetType());
            s = extraTypes != null
                ? new XmlSerializer(objectIn.GetType(), extraTypes)
                : new XmlSerializer(objectIn.GetType());

            var writer = target.CreateWriter();

            s.Serialize(writer, objectIn);

            return target;
        catch (Exception ex)
            throw new Exception(string.Format("Could not serialize object: {0}", ex.Message));

    public static T Deserialize<T>(XDocument xDocument, string defaultNamespace)
        XmlSerializer s = new XmlSerializer(typeof(T), defaultNamespace);

        T result = (T)s.Deserialize(xDocument.CreateReader());

        return result;
share|improve this answer
How can i call this method? I suppose T objectIn is my arraylist, what is the Type[] extraTypes –  user1005633 Jul 24 '12 at 17:10
add comment

Your Answer


