1

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?

Brandon
  • 191
  • 2
  • 15
user1005633
  • 625
  • 3
  • 9
  • 23
  • You really shouldn't ever use `ArrayList` anymore. You should use `List` so that you have a strongly typed collection. – Servy Jul 24 '12 at 17:11

2 Answers2

1

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.

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
0

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)
    {
        try
        {
            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);

            writer.Close();
            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;
    }
paul
  • 21,653
  • 1
  • 53
  • 54